Skip to content

Commit

Permalink
test: implemented test_Indexed, should fail
Browse files Browse the repository at this point in the history
  • Loading branch information
zonca committed Dec 5, 2023
1 parent 630db18 commit 8269215
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions header-only/tests/test_1494-layout-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ using TupleBuilder = awkward::LayoutBuilder::Tuple<BUILDERS...>;
template <unsigned SIZE, class BUILDER>
using RegularBuilder = awkward::LayoutBuilder::Regular<SIZE, BUILDER>;

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

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

Expand Down Expand Up @@ -1209,6 +1212,62 @@ test_Regular_size0() {
assert(builder.length() == 0);
}

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

std::cout << "Executing test_Indexed";

auto& subbuilder = builder.append_index();
subbuilder.append(1.1);

builder.append_index();
subbuilder.append(2.2);

double data[3] = {3.3, 4.4, 5.5};

builder.extend_index(1);
subbuilder.extend(data, 3);

// [1.1, 2.2, 3.3, 4.4, 5.5]

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

std::map<std::string, size_t> names_nbytes = {};
builder.buffer_nbytes(names_nbytes);
assert(names_nbytes.size() == 2);

auto buffers = empty_buffers(names_nbytes);
builder.to_buffers(buffers);

std::ostringstream out;
dump(out,
"node0-index", (uint32_t*)buffers["node0-index"], names_nbytes["node0-index"]/sizeof(uint32_t),
"node1-data", (double*)buffers["node1-data"], names_nbytes["node1-data"]/sizeof(double));

std::string check{"node0-index: 0 1 2 3 4 \n"
"node1-data: 1.1 2.2 3.3 4.4 5.5 \n"};
assert(out.str().compare(check) == 0);

assert(builder.form() ==
"{ "
"\"class\": \"IndexedArray\", "
"\"index\": \"u32\", "
"\"content\": { "
"\"class\": \"NumpyArray\", "
"\"primitive\": \"float64\", "
"\"form_key\": \"node1\" "
"}, "
"\"form_key\": \"node0\" "
"}");

clear_buffers(buffers);
builder.clear();
assert(builder.length() == 0);
}

void
test_Indexed_as_IndexedOption() {
IndexedOptionBuilder<uint32_t, NumpyBuilder<double>> builder;
Expand Down Expand Up @@ -1875,6 +1934,7 @@ int main(int /* argc */, char ** /* argv */) {
test_Regular();
test_Regular_size0();
test_Indexed_as_IndexedOption();
test_Indexed();
test_IndexedOption();
test_IndexedOption_Record();
test_Unmasked();
Expand Down

0 comments on commit 8269215

Please sign in to comment.