File: test_3091-layout-builder-is-valid.cpp

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (142 lines) | stat: -rw-r--r-- 3,943 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

#include "awkward/LayoutBuilder.h"

// if BUG_FIXED is false, the tests confirm the presence of the bugs in is_valid
const bool BUG_FIXED=1;

template<class PRIMITIVE, class BUILDER>
using IndexedBuilder = awkward::LayoutBuilder::Indexed<PRIMITIVE, BUILDER>;

template<class PRIMITIVE, class BUILDER>
using IndexedOptionBuilder = awkward::LayoutBuilder::IndexedOption<PRIMITIVE, BUILDER>;

template<class PRIMITIVE>
using StringBuilder = awkward::LayoutBuilder::String<PRIMITIVE>;

void
test_Indexed_categorical() {
  IndexedBuilder<uint32_t, StringBuilder<double>> builder;
  assert(builder.length() == 0);
  builder.set_parameters("\"__array__\": \"categorical\"");

  auto& subbuilder = builder.append_index(0);
  builder.append_index(1);

  subbuilder.append("zero");
  subbuilder.append("one");

  std::string error;
  assert(builder.is_valid(error) == true);

  // index and content could have different lengths
  builder.append_index(1);
  assert(builder.is_valid(error) == BUG_FIXED);
}

void
test_Indexed_categorical_invalid_index() {
  IndexedBuilder<uint32_t, StringBuilder<double>> builder;
  assert(builder.length() == 0);
  builder.set_parameters("\"__array__\": \"categorical\"");

  auto& subbuilder = builder.append_index(0);
  builder.append_index(1);

  subbuilder.append("zero");
  subbuilder.append("one");

  std::string error;
  assert(builder.is_valid(error) == true);

  // index should be less than the length of content
  subbuilder.append("two");
  builder.append_index(9);
  bool assertion = builder.is_valid(error) == !BUG_FIXED;
  // std::cout << error << std::endl;
  assert(assertion);
}

void
test_IndexedOption_categorical() {
  IndexedOptionBuilder<int32_t, StringBuilder<double>> builder;
  assert(builder.length() == 0);
  builder.set_parameters("\"__array__\": \"categorical\"");

  builder.append_invalid();
  auto& subbuilder = builder.append_valid(1);
  subbuilder.append("zero");
  builder.append_valid(1);
  subbuilder.append("one");

  std::string error;
  bool assertion = builder.is_valid(error);
  // std::cout << error << std::endl;
  assert(assertion);

  // index and content could have different lengths
  builder.append_valid(1);
  builder.append_valid(1);
  builder.append_valid(1);
  assert(builder.is_valid(error) == BUG_FIXED);
}

void
test_IndexedOption_categorical_invalid_index() {
  IndexedOptionBuilder<int32_t, StringBuilder<double>> builder;
  assert(builder.length() == 0);
  builder.set_parameters("\"__array__\": \"categorical\"");

  builder.append_invalid();
  auto& subbuilder = builder.append_valid(1);
  subbuilder.append("zero");
  builder.append_valid(1);
  subbuilder.append("one");

  std::string error;
  bool assertion = builder.is_valid(error);
  // std::cout << error << std::endl;
  assert(assertion);

  // index should be less than the length of content
  builder.append_valid(9);
  subbuilder.append("two");
  assertion = builder.is_valid(error) == !BUG_FIXED;
  // std::cout << error << std::endl;
  assert(assertion);
}

void
test_Indexed_empty() {
  IndexedBuilder<uint32_t, StringBuilder<double>> builder;
  assert(builder.length() == 0);

  // empty indexed builder should be valid
  std::string error;
  assert(builder.is_valid(error));
}

void
test_IndexedOption_empty() {
  IndexedOptionBuilder<uint32_t, StringBuilder<double>> builder;
  assert(builder.length() == 0);

  // empty indexed builder should be valid
  std::string error;
  assert(builder.is_valid(error));

  // content has length 0 but still should be valid
  builder.append_invalid();
  assert(builder.is_valid(error));
}

int main(int /* argc */, char ** /* argv */) {
  test_Indexed_categorical();
  test_Indexed_categorical_invalid_index();
  test_IndexedOption_categorical();
  test_IndexedOption_categorical_invalid_index();
  test_IndexedOption_empty();
  test_Indexed_empty();

  return 0;
}