File: lib_common_strncat.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 (52 lines) | stat: -rw-r--r-- 1,743 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
#include <string.h>
#include "unittest.h"

#define SourceStringSize 384                            // test correct page passing (>256, multiple of 128 here)

static char SourceString[SourceStringSize+1];           // +1 room for terminating null
static char DestinationString[2*SourceStringSize+1];    // will contain two times the source buffer


TEST
{
    unsigned i;
    char*    p;
    
    for (i=0; i < SourceStringSize; ++i)
      SourceString[i] = (i%128)+1;

    SourceString[i] = 0;

    ASSERT_AreEqual(SourceStringSize, strlen(SourceString), "%u", "Source string initialization or 'strlen()' problem!");

    /* Ensure empty destination string */
    DestinationString[0] = 0;

    ASSERT_AreEqual(0, strlen(DestinationString), "%u", "Destination string initialization or 'strlen()' problem!");
    
    /* Test "unlimted" concatenation to empty buffer */

    strncat(DestinationString, SourceString, 1024);
    
    ASSERT_AreEqual(SourceStringSize, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to empty buffer!");
    
    /* Test limited concatenation to non empty buffer */

    p = strncat(DestinationString, SourceString, 128);

    ASSERT_AreEqual(SourceStringSize+128, strlen(DestinationString), "%u", "Unexpected string length while string concatenation to non-empty buffer!");

    /* Test return value */

    ASSERT_IsTrue(p == DestinationString, "Invalid return value!");

    /* Test contents */

    for(i=0; i < strlen(DestinationString); ++i)
    {
        unsigned current = DestinationString[i];
        unsigned expected = (i%128)+1;
        ASSERT_AreEqual(expected, current, "%u", "Unexpected destination buffer contents at position %u!\n" COMMA i);
    }
}
ENDTEST