-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: is_valid checks in header-only library (#3091)
* feat: set an explicit index in Indexed and IndexedOption Useful in particular for categorical builders * test: new tests checking presence of a bug in is_valid * style: pre-commit fixes * test: renamed test file with pull request number * test: use const instead of define * test: use ! instead of not operator (issue with Windows) * test: switch bug_fixed to 1, tests will fail until bug fixed * fix: reimplement is_valid to check max of index_ against len of content * test: print out error message * fix: is_valid in IndexedOption now checks index_ is less than len of content * test: IndexOption should have signed index to accommodate -1 * test: print out error from is_valid * fix: reimplemented is_valid for Union, now checks index is below length of content for each tag * feat: implement max_index tracking in Indexed * fix: set max_index_ also in extend_index * feat: append_valid() now calls append_valid(i) * fix: set max_index_ in extend_valid * feat: implement is_valid with max_index_ * feat: check index of Indexed is >0, if not set max_index_ to max int * feat: custom error message for negative index --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7f1d261
commit 082e1b7
Showing
3 changed files
with
191 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
header-only/tests/test_3091-layout-builder-is-valid.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// 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); | ||
} | ||
|
||
int main(int /* argc */, char ** /* argv */) { | ||
test_Indexed_categorical(); | ||
test_Indexed_categorical_invalid_index(); | ||
test_IndexedOption_categorical(); | ||
test_IndexedOption_categorical_invalid_index(); | ||
|
||
return 0; | ||
} |