File: test_bsearch_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 (145 lines) | stat: -rw-r--r-- 4,034 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*------------------------------------------------------------------
 * test_bsearch_s
 * File 'misc/bsearch_s.c'
 * Lines executed:89.66% of 29
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_lib.h"
#include <stdlib.h>

#ifdef HAVE_BSEARCH_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"
int test_bsearch_s(void);

#define LEN 10
/* needs to be sorted! */
struct items {
    int iv;
    const char *sv;
} array[] = {{0, "zero"},  {1, "one"},  {2, "two"}, {3, "three"},
             {4, "four"},  {5, "five"}, {6, "six"}, {7, "seven"},
             {8, "eight"}, {9, "nine"}, {10, "ten"}};

static int comp(const void *ptr1, const void *ptr2, void *ctx) {
    struct items *i1 = (struct items *)ptr1;
    struct items *i2 = (struct items *)ptr2;
    if (ctx) {
        errno = -1;
        return 0; /* shortcut just for testing */
    }
    return i1->iv > i2->iv ? 1 : i1->iv == i2->iv ? 0 : -1;
}

int test_bsearch_s(void) {
    int errs = 0;
#ifndef HAVE_CT_BOS_OVR
    int have_wine = 0;
#endif
    int i;
    struct items key;
    struct items *item;
    void *vitem;

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

    print_msvcrt(use_msvcrt);

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty buf or bufsize")
    vitem = bsearch_s(NULL, array, LEN, 0, comp, NULL);
    init_msvcrt(errno == ESNULLP, &use_msvcrt);
    ERRNO_MSVC(ESNULLP, EINVAL);
    PTRNULL(vitem);

    EXPECT_BOS("empty buf or bufsize")
    vitem = bsearch_s(&key, NULL, LEN, sizeof(array[0]), comp, NULL);
    if (use_msvcrt && errno == EOF) {
        printf("Using wine\n");
        have_wine = 1;
    }
    ERRNO_MSVC(ESNULLP, have_wine ? EOF : EINVAL);
    if (!have_wine)
        PTRNULL(vitem);

    EXPECT_BOS("empty compar")
    vitem = bsearch_s(&key, (const void *)array, LEN, sizeof(array[0]), NULL, NULL);
    ERRNO_MSVC(ESNULLP, EINVAL);
    PTRNULL(vitem);

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

    EXPECT_BOS("base overflow")
    vitem = bsearch_s(&key, array, RSIZE_MAX_MEM + 1, sizeof(array[0]), comp, NULL);
    ERRNO_MSVC(ESNOSPC, EOF);
    if (!use_msvcrt)
        PTRNULL(vitem);

    EXPECT_BOS("base overflow")
    vitem = bsearch_s(&key, array, LEN, RSIZE_MAX_MEM + 1, comp, NULL);
    ERRNO_MSVC(ESNOSPC, EOF);
    if (!use_msvcrt)
        PTRNULL(vitem);
#endif
    /*--------------------------------------------------*/

    /* allow empty array? msvcrt disallows it */
    key.iv = 0;
    vitem = bsearch_s(&key, NULL, 0, 0, comp, NULL);
    PTRNULL(vitem);
    ERRNO_MSVC(0, EINVAL);

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

    /* find em */
    for (i = 0; i < LEN; ++i) {
        key.iv = array[i].iv;
        item = (struct items *)bsearch_s(&key, array, LEN, sizeof(array[0]),
                                         comp, NULL);
        PTRNN(item);
        if (item != &array[i] && !use_msvcrt) {
            PTRNULL(item);
        }
        ERRNO(use_msvcrt ? EOF : 0);
    }

    /* cannot find em */
    key.iv = -1;
    item = (struct items *)bsearch_s(&key, array, LEN, sizeof(array[0]), comp,
                                     NULL);
    if (!use_msvcrt)
        PTRNULL(item);
    ERRNO(use_msvcrt ? EOF : 0);

    key.iv = 11;
    item = (struct items *)bsearch_s(&key, array, LEN, sizeof(array[0]), comp,
                                     NULL);
    if (!use_msvcrt)
        PTRNULL(item);
    ERRNO(use_msvcrt ? EOF : 0);

    /* special case for 3rd compar arg ctx */
    key.iv = 1;
    item = (struct items *)bsearch_s(&key, array, LEN, sizeof(array[0]), comp,
                                     array);
    PTRNN(item);
    ERRNO(-1);

    key.iv = 10;
    item = (struct items *)bsearch_s(&key, array, LEN, sizeof(array[0]), comp,
                                     array);
    PTRNN(item);
    ERRNO(-1);

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

    return (errs);
}

int main(void) { return (test_bsearch_s()); }