File: test.cc

package info (click to toggle)
re2c 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 42,604 kB
  • sloc: cpp: 12,993; sh: 1,614; haskell: 437; makefile: 311; ansic: 56
file content (106 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download
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
#include <limits>
#include <stdio.h>

#include "src/util/s_to_n32_unsafe.h"

namespace re2c_test {

static const uint32_t DIGITS = 256;

// writes string backwards and returns pointer to the start
// no terminating null as we don't need it
static char * u64_to_s_fastest_ever (uint64_t u, char * s)
{
    if (u == 0) {
        *--s = '0';
    }
    else {
        while (u > 0) {
            const uint64_t d = u % 10 + '0';
            *--s = static_cast<char> (d);
            u /= 10;
        }
    }
    return s;
}

static int32_t test_u (uint64_t i)
{
    char s [DIGITS];
    char * const s_end = s + DIGITS;
    char * const s_start = u64_to_s_fastest_ever (i, s_end);
    uint32_t u = i == 0; // not equal to i
    if (s_to_u32_unsafe (s_start, s_end, u) && u != i)
    {
        fprintf (stderr, "unsigned: expected: %lu, got: %u\n", i, u);
        return 1;
    }
    return 0;
}

static int32_t test_i (int64_t i)
{
    char s [DIGITS];
    char * const s_end = s + DIGITS;
    const uint64_t i_abs = i < 0
        ? static_cast<uint64_t> (-i)
        : static_cast<uint64_t> (i);
    char * s_start = u64_to_s_fastest_ever (i_abs, s_end);
    if (i < 0)
    {
        *--s_start = '-';
    }
    int32_t j = i == 0; // not equal to i
    if (s_to_i32_unsafe (s_start, s_end, j) && j != i)
    {
        fprintf (stderr, "signed: expected: %ld, got: %d\n", i, j);
        return 1;
    }
    return 0;
}

static int32_t test ()
{
    int32_t ok = 0;

    static const uint64_t UDELTA = 0xFFFF;
    // zero neighbourhood
    for (uint64_t i = 0; i <= UDELTA; ++i)
    {
        ok |= test_u (i);
    }
    // u32_max neighbourhood
    static const uint64_t u32_max = std::numeric_limits<uint32_t>::max();
    for (uint64_t i = u32_max - UDELTA; i <= u32_max + UDELTA; ++i)
    {
        ok |= test_u (i);
    }

    static const int64_t IDELTA = 0xFFFF;
    // i32_min neighbourhood
    static const int64_t i32_min = std::numeric_limits<int32_t>::min();
    for (int64_t i = i32_min - IDELTA; i <= i32_min + IDELTA; ++i)
    {
        ok |= test_i (i);
    }
    // zero neighbourhood
    for (int64_t i = -IDELTA; i <= IDELTA; ++i)
    {
        ok |= test_i (i);
    }
    // i32_max neighbourhood
    static const int64_t i32_max = std::numeric_limits<int32_t>::max();
    for (int64_t i = i32_max - IDELTA; i <= i32_max + IDELTA; ++i)
    {
        ok |= test_i (i);
    }

    return ok;
}

} // namespace re2c_test

int main ()
{
    return re2c_test::test ();
}