src
test.cc
Go to the documentation of this file.
1 #include <limits>
2 #include <stdio.h>
3 
5 
6 namespace re2c_test {
7 
8 static const uint32_t DIGITS = 256;
9 
10 // writes string backwards and returns pointer to the start
11 // no terminating null as we don't need it
12 static char * u64_to_s_fastest_ever (uint64_t u, char * s)
13 {
14  while (u > 0)
15  {
16  const uint64_t d = u % 10 + '0';
17  *--s = static_cast<char> (d);
18  u /= 10;
19  }
20  return s;
21 }
22 
23 static int32_t test_u (uint64_t i)
24 {
25  char s [DIGITS];
26  char * const s_end = s + DIGITS;
27  char * const s_start = u64_to_s_fastest_ever (i, s_end);
28  uint32_t u = i == 0; // not equal to i
29  if (s_to_u32_unsafe (s_start, s_end, u) && u != i)
30  {
31  fprintf (stderr, "unsigned: expected: %lu, got: %u\n", i, u);
32  return 1;
33  }
34  return 0;
35 }
36 
37 static int32_t test_i (int64_t i)
38 {
39  char s [DIGITS];
40  char * const s_end = s + DIGITS;
41  const uint64_t i_abs = i < 0
42  ? static_cast<uint64_t> (-i)
43  : static_cast<uint64_t> (i);
44  char * s_start = u64_to_s_fastest_ever (i_abs, s_end);
45  if (i < 0)
46  {
47  *--s_start = '-';
48  }
49  int32_t j = i == 0; // not equal to i
50  if (s_to_i32_unsafe (s_start, s_end, j) && j != i)
51  {
52  fprintf (stderr, "signed: expected: %ld, got: %d\n", i, j);
53  return 1;
54  }
55  return 0;
56 }
57 
58 static int32_t test ()
59 {
60  int32_t ok = 0;
61 
62  static const uint64_t UDELTA = 0xFFFF;
63  // zero neighbourhood
64  for (uint64_t i = 0; i <= UDELTA; ++i)
65  {
66  ok |= test_u (i);
67  }
68  // u32_max neighbourhood
69  static const uint64_t u32_max = std::numeric_limits<uint32_t>::max();
70  for (uint64_t i = u32_max - UDELTA; i <= u32_max + UDELTA; ++i)
71  {
72  ok |= test_u (i);
73  }
74 
75  static const int64_t IDELTA = 0xFFFF;
76  // i32_min neighbourhood
77  static const int64_t i32_min = std::numeric_limits<int32_t>::min();
78  for (int64_t i = i32_min - IDELTA; i <= i32_min + IDELTA; ++i)
79  {
80  ok |= test_i (i);
81  }
82  // zero neighbourhood
83  for (int64_t i = -IDELTA; i <= IDELTA; ++i)
84  {
85  ok |= test_i (i);
86  }
87  // i32_max neighbourhood
88  static const int64_t i32_max = std::numeric_limits<int32_t>::max();
89  for (int64_t i = i32_max - IDELTA; i <= i32_max + IDELTA; ++i)
90  {
91  ok |= test_i (i);
92  }
93 
94  return ok;
95 }
96 
97 } // namespace re2c_test
98 
99 int main ()
100 {
101  return re2c_test::test ();
102 }
static int32_t test_i(int64_t i)
Definition: test.cc:37
static const uint32_t DIGITS
Definition: test.cc:8
static int32_t test()
Definition: test.cc:68
static int32_t test_u(uint64_t i)
Definition: test.cc:23
int main()
Definition: test.cc:91
bool s_to_u32_unsafe(const char *s, const char *s_end, uint32_t &number)
static char * u64_to_s_fastest_ever(uint64_t u, char *s)
Definition: test.cc:12
bool s_to_i32_unsafe(const char *s, const char *s_end, int32_t &number)