File: test_strfirstchar_s.c

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (115 lines) | stat: -rw-r--r-- 3,466 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
/*------------------------------------------------------------------
 * test_strfirstchar_s
 * File 'extstr/strfirstchar_s.c'
 * Lines executed:93.33% of 15
 *
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"

#define LEN (128)
#define SHORT_LEN (5)

#define EXPFIRST(x)                                                            \
    if (rc != x) {                                                             \
        debug_printf("%s %u  Error  str1=%p  first=%p  rc=%d \n",              \
                     __FUNCTION__, __LINE__, (void *)str1, (void *)first, rc); \
        errs++;                                                                \
    }
#define NOFIRST()                                                              \
    if (first) {                                                               \
        debug_printf("%s %u  Error  str1=%p  first=%p  rc=%d \n",              \
                     __FUNCTION__, __LINE__, (void *)str1, (void *)first, rc); \
        errs++;                                                                \
    }

int main(void) {
    errno_t rc;
    rsize_t len;

    char *first;
    char str1[LEN];
    int errs = 0;

    /*--------------------------------------------------*/

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty dest")
    rc = strfirstchar_s(NULL, LEN, 'a', &first);
    EXPFIRST(ESNULLP)
    NOFIRST()
    /*--------------------------------------------------*/

    EXPECT_BOS("empty firstp")
    rc = strfirstchar_s(str1, LEN, 'a', NULL);
    EXPFIRST(ESNULLP)

    /*--------------------------------------------------*/

    EXPECT_BOS("empty dest or dmax")
    rc = strfirstchar_s(str1, 0, 'a', &first);
    EXPFIRST(ESZEROL)
    NOFIRST()

    EXPECT_BOS("dest overflow")
    rc = strfirstchar_s(str1, RSIZE_MAX_STR + 1, 'a', &first);
    EXPFIRST(ESLEMAX)
    NOFIRST()

#endif
    /*--------------------------------------------------*/

    str1[0] = '\0';

    rc = strfirstchar_s(str1, LEN, 'a', &first);
    EXPFIRST(ESNOTFND)
    NOFIRST()

    /*--------------------------------------------------*/

    strcpy(str1, "Keep it simple");

    rc = strfirstchar_s(str1, LEN, 'z', &first);
    EXPFIRST(ESNOTFND)
    NOFIRST()
    /*--------------------------------------------------*/

    strcpy(str1, "kEEp it simple");

    rc = strfirstchar_s(str1, LEN, 'E', &first);
    ERR(EOK)
    if (first != &str1[1]) {
        debug_printf("%s %u  Error  str1=%p  first=%p  rc=%d \n", __FUNCTION__,
                     __LINE__, (void *)str1, (void *)first, rc);
        errs++;
    }
    /*--------------------------------------------------*/

    strcpy(str1, "keep it simpleZ");
    len = strlen(str1);

    rc = strfirstchar_s(str1, len, 'Z', &first);
    ERR(EOK)
    if (first != &str1[14]) {
        debug_printf("%s %u  Error  str1=%p  first=%p  rc=%d \n", __FUNCTION__,
                     __LINE__, (void *)str1, (void *)first, rc);
        errs++;
    }
    /*--------------------------------------------------*/

    strcpy(str1, "keep it simpleZZ");

    rc = strfirstchar_s(str1, LEN, 'Z', &first);
    ERR(EOK)
    if (first != &str1[14]) {
        debug_printf("%s %u  Error  str1=%p  first=%p  rc=%d \n", __FUNCTION__,
                     __LINE__, (void *)str1, (void *)first, rc);
        errs++;
    }
    /*--------------------------------------------------*/

    return (errs);
}