File: validate_ascii_with_errors_tests.cpp

package info (click to toggle)
simdutf 8.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,524 kB
  • sloc: cpp: 64,498; ansic: 15,347; python: 3,592; sh: 366; makefile: 12
file content (77 lines) | stat: -rw-r--r-- 2,296 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
#include "simdutf.h"

#include <tests/helpers/fixed_string.h>
#include <tests/helpers/random_utf8.h>
#include <tests/helpers/test.h>

TEST_LOOP(no_error_ASCII) {
  simdutf::tests::helpers::random_utf8 generator{seed, 1, 0, 0, 0};
  const auto ascii{generator.generate(512)};

  simdutf::result res = implementation.validate_ascii_with_errors(
      reinterpret_cast<const char *>(ascii.data()), ascii.size());

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

TEST_LOOP(error_ASCII) {
  simdutf::tests::helpers::random_utf8 generator{seed, 1, 0, 0, 0};

  auto ascii{generator.generate(512)};

  for (unsigned int i = 0; i < ascii.size(); i++) {
    ascii[i] += 0b10000000;

    simdutf::result res = implementation.validate_ascii_with_errors(
        reinterpret_cast<const char *>(ascii.data()), ascii.size());

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

    ascii[i] -= 0b10000000;
  }
}

#if SIMDUTF_CPLUSPLUS23

namespace {
// for negative compilation tests
template <class InputPtr>
concept passable_to_validate = requires(InputPtr p) {
  simdutf::scalar::ascii::validate_with_errors(p, 10u);
};

} // namespace

TEST(compile_time_valid) {

  using namespace simdutf::tests::helpers;
  constexpr auto ascii = "a normal ascii text"_latin1;

  static_assert(simdutf::validate_ascii_with_errors(ascii).is_ok());
  static_assert(
      simdutf::validate_ascii_with_errors(ascii.as_array<unsigned char>())
          .is_ok());
  static_assert(
      simdutf::validate_ascii_with_errors(ascii.as_array<signed char>())
          .is_ok());
  static_assert(
      simdutf::validate_ascii_with_errors(ascii.as_array<std::byte>()).is_ok());

  static_assert(passable_to_validate<char *>);
  static_assert(passable_to_validate<unsigned char *>);
  static_assert(passable_to_validate<const char *>);
  static_assert(!passable_to_validate<int *>);
  static_assert(passable_to_validate<std::array<char, 10>>);
  static_assert(!passable_to_validate<std::array<int, 10>>);
}

TEST(compile_time_invalid) {
  using namespace simdutf::tests::helpers;
  constexpr auto not_ascii = u8"not ascii: köttbulle"_utf8;
  static_assert(simdutf::validate_ascii_with_errors(not_ascii).is_err());
}
#endif

TEST_MAIN