summaryrefslogtreecommitdiff
path: root/modules/codec-netcdf/tests/codec-netcdf.cpp
blob: 7e624df743ff45a69f1b5104120397fe4f40c2d0 (plain)
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
#include <forstio/test/suite.hpp>
#include "../c++/netcdf.hpp"

namespace {
namespace schema {
using namespace saw::schema;
using TestStruct = Struct<
		Member<Int32, "data">,
		Member<Float64, "other">
>;

using TestArrayStruct = Struct<
		Member<Array<Int32,2>, "data">
>;
}

std::array<uint8_t,112> tests_data_primitive_nc = {
  0x43, 0x44, 0x46, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
  0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04,
  0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x05, 0x6f, 0x74, 0x68, 0x65,
  0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08,
  0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x05, 0x40, 0x40, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00
};

std::array<uint8_t,156> tests_data_array_nc = {
  0x43, 0x44, 0x46, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a,
  0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x78, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x79, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04,
  0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x60,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05,
  0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x08,
  0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0b,
  0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0e
};

SAW_TEST("NetCDF Struct Primitive write"){
	using namespace saw;

	data<schema::TestStruct, encode::Native> native;

	native.template get<"data">().set(5);
	native.template get<"other">().set(32.0);

	codec<schema::TestStruct, encode::Netcdf> codec;

	data<schema::TestStruct, encode::Netcdf> netcdf;

	auto eov = codec.encode(native, netcdf);
	SAW_EXPECT(eov.is_value(), "Encoding failed");
	auto& nc_data = netcdf.get_data();
	SAW_EXPECT(nc_data.size() == tests_data_primitive_nc.size(), "Incorrect size");

	for(std::size_t i = 0; i < nc_data.size(); ++i){
		SAW_EXPECT(nc_data.at(i) == tests_data_primitive_nc.at(i), "Incorrect Value");
	}
}

SAW_TEST("NetCDF Struct Primitive read"){
		using namespace saw;
		
		data<schema::TestStruct, encode::Netcdf> netcdf{tests_data_primitive_nc};
		data<schema::TestStruct, encode::Native> native;

		codec<schema::TestStruct, encode::Netcdf> codec;

		auto eov = codec.decode(netcdf, native);
		SAW_EXPECT(eov.is_value(), "Decoding failed");
		SAW_EXPECT(native.template get<"data">().get() == 5, "Int Value incorrect");
		SAW_EXPECT(native.template get<"other">().get() == 32.0, "Double Value incorrect");
}

SAW_TEST("NetCDF Struct Array read"){
		using namespace saw;
		
		data<schema::TestArrayStruct, encode::Netcdf> netcdf{tests_data_array_nc};

		data<schema::TestArrayStruct, encode::Native> native;

		codec<schema::TestArrayStruct, encode::Netcdf> codec;

		auto eov = codec.decode(netcdf, native);
		SAW_EXPECT(eov.is_value(), "Decoding failed");
		auto& arr = native.template get<"data">();
		SAW_EXPECT(arr.get_dim_size(0) == 5, "Incorrect dimension 0");
		SAW_EXPECT(arr.get_dim_size(1) == 3, "Incorrect dimension 1");

		for(std::size_t i = 0; i < 5; ++i){
				for(std::size_t j = 0; j < 3; ++j){
						int64_t exp_val = i * 3 + j;
						SAW_EXPECT(arr.at(i,j).get() == exp_val, "Incorrect value");
				}
		}
}
}