]> git.sur5r.net Git - cc65/blob - testcode/lib/strcat-test.c
2e00b80cbfcb64cf9959cb79727920d359bf6cc3
[cc65] / testcode / lib / strcat-test.c
1 #include <unittest.h>
2 #include <string.h>
3
4 #define SourceStringSize 257                            // test correct page passing (>256)
5
6 static char SourceString[SourceStringSize+1];           // +1 room for terminating null
7 static char DestinationString[2*SourceStringSize+1];    // will contain two times the source buffer
8
9
10 TEST
11 {
12     unsigned i,j;
13     char*    p;
14     
15     for (i=0; i < SourceStringSize; ++i)
16       SourceString[i] = (i%128)+1;
17
18     SourceString[i] = 0;
19
20     ASSERT_AreEqual(SourceStringSize, strlen(SourceString), "%u", "Source string initialization or 'strlen()' problem!");
21
22     /* Ensure empty destination string */
23     DestinationString[0] = 0;
24
25     ASSERT_AreEqual(0, strlen(DestinationString), "%u", "Destination string initialization or 'strlen()' problem!");
26     
27     /* Test concatenation to empty buffer */
28
29     strcat(DestinationString, SourceString);
30     
31     ASSERT_AreEqual(SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to empty buffer!");
32     
33     /* Test concatenation to non empty buffer */
34
35     p = strcat(DestinationString, SourceString);
36
37     ASSERT_AreEqual(2*SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to non-empty buffer!");
38
39     /* Test return value */
40
41     ASSERT_IsTrue(p == DestinationString,"Invalid return value!");
42
43     /* Test contents */
44
45     for(j=0; j <2; ++j)
46         for(i=0; i < SourceStringSize; ++i)
47         {
48             unsigned position = j*SourceStringSize+i;
49             unsigned current = DestinationString[position];
50             unsigned expected = (i%128)+1;
51             ASSERT_AreEqual(expected, current, "%u", "Unexpected destination buffer contents at position %u!\n" COMMA position);
52         }
53 }
54 ENDTEST
55
56