File: validate_utf32_with_errors_tests.cpp

package info (click to toggle)
simdutf 7.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,244 kB
  • sloc: cpp: 60,074; ansic: 14,226; python: 3,364; sh: 321; makefile: 12
file content (91 lines) | stat: -rw-r--r-- 3,088 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
#include "simdutf.h"

#include <tests/helpers/random_utf32.h>
#include <tests/helpers/test.h>

TEST(issue_531) {
  const char32_t data[] = {0xdbdb,     0xff380000, 0xffffffff, 0xffffffff,
                           0xffffffff, 0xffffffff, 0x00ff,     0x0000,
                           0x0000,     0x0000,     0x0000,     0x0000,
                           0x0000,     0x0000,     0x0000,     0x0000,
                           0x0000,     0x0000,     0x0000,     0x0000};
  constexpr std::size_t data_len = sizeof(data);
  const auto validation1 = implementation.validate_utf32_with_errors(
      (const char32_t *)data, data_len);
  // got return [count=1, error=TOO_LARGE] from implementation rvv
  // got return [count=0, error=SURROGATE] from implementation fallback
  ASSERT_EQUAL(validation1.error, simdutf::error_code::SURROGATE);
  ASSERT_EQUAL(validation1.count, 0);
}

TEST_LOOP(validate_utf32_with_errors_returns_success_for_valid_input) {
  simdutf::tests::helpers::random_utf32 generator{seed};
  const auto utf32{generator.generate(256, seed)};

  simdutf::result res = implementation.validate_utf32_with_errors(
      reinterpret_cast<const char32_t *>(utf32.data()), utf32.size());

  ASSERT_EQUAL(res.error, simdutf::error_code::SUCCESS);
  ASSERT_EQUAL(res.count, utf32.size());
}

TEST(validate_utf32_with_errors_returns_success_for_empty_string) {
  const char32_t *buf = (char32_t *)"";

  simdutf::result res = implementation.validate_utf32_with_errors(buf, 0);

  ASSERT_EQUAL(res.error, simdutf::error_code::SUCCESS);
  ASSERT_EQUAL(res.count, 0);
}

TEST_LOOP(

    validate_utf32_with_errors_returns_error_when_input_in_forbidden_range) {
  simdutf::tests::helpers::random_utf32 generator{seed};

  auto utf32{generator.generate(128)};
  const char32_t *buf = reinterpret_cast<const char32_t *>(utf32.data());
  const size_t len = utf32.size();

  for (char32_t wrong_value = 0xd800; wrong_value <= 0xdfff; wrong_value++) {
    for (size_t i = 0; i < utf32.size(); i++) {
      const char32_t old = utf32[i];
      utf32[i] = wrong_value;

      simdutf::result res = implementation.validate_utf32_with_errors(buf, len);

      ASSERT_EQUAL(res.error, simdutf::error_code::SURROGATE);
      ASSERT_EQUAL(res.count, i);

      utf32[i] = old;
    }
  }
}

TEST_LOOP(validate_utf32_with_errors_returns_error_when_input_too_large) {
  simdutf::tests::helpers::random_utf32 generator{seed};

  std::uniform_int_distribution<uint32_t> bad_range{0x110000, 0xffffffff};
  std::mt19937 gen{seed};

  auto utf32{generator.generate(128)};
  const char32_t *buf = reinterpret_cast<const char32_t *>(utf32.data());
  const size_t len = utf32.size();

  for (size_t r = 0; r < 1000; r++) {
    uint32_t wrong_value = bad_range(gen);
    for (size_t i = 0; i < utf32.size(); i++) {
      const char32_t old = utf32[i];
      utf32[i] = wrong_value;

      simdutf::result res = implementation.validate_utf32_with_errors(buf, len);

      ASSERT_EQUAL(res.error, simdutf::error_code::TOO_LARGE);
      ASSERT_EQUAL(res.count, i);

      utf32[i] = old;
    }
  }
}

TEST_MAIN