File: strdup-test.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (115 lines) | stat: -rw-r--r-- 2,024 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


/* From _heap.h */
extern unsigned _horg;          /* Bottom of heap */
extern unsigned _hptr;          /* Current top */
extern unsigned _hend;          /* Upper limit */
extern unsigned _hfirst;        /* First free block in list */
extern unsigned _hlast;         /* Last free block in list */


static unsigned char* V[256];



static void ShowInfo (void)
/* Show heap info */
{
    /* Count free blocks */
    unsigned Count = 0;
    unsigned** P = (unsigned**) _hfirst;
    while (P) {
        ++Count;
        P = P[1];
    }
    printf ("%04X  %04X  %04X  %04X  %04X %u\n",
            _horg, _hptr, _hend, _hfirst, _hlast, Count);

    if (Count) {
        P = (unsigned**) _hfirst;
        while (P) {
            printf ("%04X  %04X  %04X %04X(%u)\n",
                    (unsigned) P, P[2], P[1], P[0], P[0]);
            P = P[1];
        }
        getchar ();
    }
}



static const char* RandStr (void)
/* Create a random string */
{
    static char S [300];
    unsigned Len = (rand () & 0xFF) + (sizeof (S) - 0xFF - 1);
    unsigned I;
    char C;

    for (I = 0; I < Len; ++I) {
        do {
            C = rand() & 0xFF;
        } while (C == 0);
        S[I] = C;
    }
    S[Len] = '\0';

    return S;
}



static void FillArray (void)
/* Fill the string array */
{
    unsigned char I = 0;
    do {
        V[I] = strdup (RandStr ());
        ++I;
    } while (I != 0);
}



static void FreeArray (void)
/* Free all strings in the array */
{
    unsigned char I = 0;
    do {
        free (V[I]);
        ++I;
    } while (I != 0);
}



int main (void)
{
    unsigned long T;

    /* Show info at start */
    ShowInfo ();

    /* Remember the time */
    T = clock ();

    /* Do the tests */
    FillArray ();
    ShowInfo ();
    FreeArray ();
    ShowInfo ();

    /* Calculate the time and print it */
    T = clock () - T;
    printf ("Time needed: %lu ticks\n", T);

    /* Done */
    return EXIT_SUCCESS;
}