File: random_utf16.cpp

package info (click to toggle)
simdutf 7.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,968 kB
  • sloc: cpp: 59,437; ansic: 14,080; python: 3,135; sh: 311; makefile: 12
file content (123 lines) | stat: -rw-r--r-- 2,835 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "random_utf16.h"
#include "simdutf.h"
#include "../reference/encode_utf16.h"

#include <stdexcept>
#include <vector>

namespace simdutf {
namespace tests {
namespace helpers {

std::vector<char16_t> random_utf16::generate_le(size_t size) {
  auto result = generate_counted(size).first;
  if (!match_system(endianness::LITTLE)) {
    change_endianness_utf16(result.data(), result.size(), result.data());
  }

  return result;
}

void random_utf16::to_ascii_le(std::vector<char16_t> &output) {
  char16_t mask = 0x7F;
  if (!match_system(endianness::LITTLE)) {
    mask = 0x7F00;
  }
  for (auto &ch : output) {
    ch &= mask;
  }
}

void random_utf16::to_ascii_be(std::vector<char16_t> &output) {
  char16_t mask = 0x7F;
  if (!match_system(endianness::BIG)) {
    mask = 0x7F00;
  }
  for (auto &ch : output) {
    ch &= mask;
  }
}

std::vector<char16_t> random_utf16::generate_be(size_t size) {
  auto result = generate_counted(size).first;
  if (!match_system(endianness::BIG)) {
    change_endianness_utf16(result.data(), result.size(), result.data());
  }

  return result;
}

std::vector<char16_t> random_utf16::generate_le(size_t size, long seed) {
  gen.seed(seed);
  return generate_le(size);
}

std::vector<char16_t> random_utf16::generate_be(size_t size, long seed) {
  gen.seed(seed);
  return generate_be(size);
}

std::pair<std::vector<char16_t>, size_t>
random_utf16::generate_counted_le(size_t size) {
  auto res = generate_counted(size);
  if (!match_system(endianness::LITTLE)) {
    change_endianness_utf16(res.first.data(), res.first.size(),
                            res.first.data());
  }

  return res;
}

std::pair<std::vector<char16_t>, size_t>
random_utf16::generate_counted_be(size_t size) {
  auto res = generate_counted(size);
  if (!match_system(endianness::BIG)) {
    change_endianness_utf16(res.first.data(), res.first.size(),
                            res.first.data());
  }

  return res;
}

std::pair<std::vector<char16_t>, size_t>
random_utf16::generate_counted(size_t size) {
  std::vector<char16_t> result;
  result.reserve(size);

  char16_t W1;
  char16_t W2;
  size_t count{0};
  while (result.size() < size) {
    count++;
    const uint32_t value = generate();
    switch (simdutf::tests::reference::utf16::encode(value, W1, W2)) {
    case 0:
      throw std::runtime_error("Random UTF-16 generator is broken");
    case 1:
      result.push_back(W1);
      break;
    case 2:
      result.push_back(W1);
      result.push_back(W2);
      break;
    }
  }
  return make_pair(result, count);
}

uint32_t random_utf16::generate() {
  switch (utf16_length(gen)) {
  case 0:
    return single_word0(gen);
  case 1:
    return single_word1(gen);
  case 2:
    return two_words(gen);
  default:
    abort();
  }
}

} // namespace helpers
} // namespace tests
} // namespace simdutf