summaryrefslogtreecommitdiff
path: root/test/test-utf.cc
blob: 7096a47ba11e365522fc0916a2bfd8a6d296accb (plain)
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
// -*- mode: c++; c-basic-offset: 2; -*-

#include "common.hh"
#include "test.hh"

#include <stdarg.h>

#include "utf.hh"

namespace {

bool test_empty() {
  ASSERT_EQ(true, valid_utf8(""));
  ASSERT_EQ(true, valid_utf8("", 0));
  ASSERT_EQ(true, valid_utf8("\xff", 0));
  auto str = "";
  ASSERT_EQ(str, read_utf8(str, 0, nullptr));
  ASSERT_EQ(str, read_utf8(str, 10, nullptr));
  return true;
}

bool test_good(char const* str, ...) {
  ASSERT_EQ(true, valid_utf8(str));
  va_list args;
  va_start(args, str);
  auto pos = str;
  auto end = str + strlen(str) + 1;
  uint32_t value;
  while (true) {
    uint32_t expected = va_arg(args, uint32_t);
    auto next = read_utf8(pos, end - pos, &value);
    if (expected == 0) {
      ASSERT_EQ(pos, next);
      ASSERT_EQ(expected, value);
      break;
    } else {
      ASSERT_EQ(false, pos == next);
      ASSERT_EQ(false, next == nullptr);
      ASSERT_EQ(expected, value);
    }
    pos = next;
  }
  va_end(args);
  return true;
}

bool test_bad(char const* str) {
  ASSERT_EQ(false, valid_utf8(str));
  return true;
}

}  // namespace

int main(void) {
  BEFORE;
  RUN(test_empty());
  RUN(test_good("$", 0x24, 0));
  RUN(test_good("\xc2\xa2", 0xa2, 0));
  RUN(test_good("\xe2\x82\xac", 0x20ac, 0));
  RUN(test_good("\xf0\x90\x8d\x88", 0x10348, 0));
  RUN(test_bad("\xf0\x82\x82\xac"));
  AFTER;
}