Debian Codegraph @

src/H5Z-SPERR/test_scripts/compactor_test.cpp

raw
1 #include "gtest/gtest.h"
2 #include <vector>
3 #include <limits>
4 #include <memory>
5
6 #include "compactor.h"
7
8 namespace {
9
10 TEST(compactor, strategy) {
11 //
12 // This test assumes that the compactor uses 32-bit integers.
13 //
14 // Create an array of all zeros
15 size_t N = 32;
16 auto buf = std::vector<unsigned int>(N, 0);
17 auto ans = compactor_strategy(buf.data(), N * sizeof(unsigned int));
18 EXPECT_EQ(ans, 0);
19
20 // Make the array of all ones
21 buf.assign(N, std::numeric_limits<unsigned int>::max());
22 ans = compactor_strategy(buf.data(), N * sizeof(unsigned int));
23 EXPECT_EQ(ans, 1);
24
25 // Make the array half half
26 for (int i = 0; i < N / 2; i++)
27 buf[i] = 0;
28 ans = compactor_strategy(buf.data(), N * sizeof(unsigned int));
29 EXPECT_EQ(ans, 0);
30 }
31
32 TEST(compactor, comp_size) {
33 //
34 // This test assumes that the compactor uses 32-bit integers.
35 //
36 // Create an array of all zeros
37 size_t N = 32;
38 auto buf = std::vector<unsigned int>(N, 0);
39 auto ans = compactor_comp_size(buf.data(), N * sizeof(unsigned int));
40 EXPECT_EQ(ans, 9);
41
42 // Make the array of all ones
43 buf.assign(N, std::numeric_limits<unsigned int>::max());
44 ans = compactor_comp_size(buf.data(), N * sizeof(unsigned int));
45 EXPECT_EQ(ans, 9);
46
47 // Make the array half half
48 for (int i = 0; i < N / 2; i++)
49 buf[i] = 0;
50 ans = compactor_comp_size(buf.data(), N * sizeof(unsigned int));
51 EXPECT_EQ(ans, 11);
52
53 // Make the array 24 0's, 8 1's.
54 for (int i = 0; i < N / 4; i++)
55 buf[i] = buf[N - 1];
56 ans = compactor_comp_size(buf.data(), N * sizeof(unsigned int));
57 EXPECT_EQ(ans, 10);
58
59 // Append the array with ints that need to be verbosely encoded.
60 buf.push_back(1);
61 buf.push_back(2);
62 ans = compactor_comp_size(buf.data(), buf.size() * sizeof(unsigned int));
63 EXPECT_EQ(ans, 18);
64 }
65
66 TEST(compactor, coding_all0_all1)
67 {
68 // an array of all 0's
69 int nbytes = 128; // 32 ints
70 auto buf = std::vector<unsigned char>(nbytes, 0);
71
72 // encode `buf`
73 auto encode = std::make_unique<unsigned char[]>(nbytes);
74 auto encode_len = compactor_encode(buf.data(), nbytes, encode.get(), nbytes);
75 EXPECT_EQ(encode_len, compactor_comp_size(buf.data(), nbytes));
76
77 // decode and test all 0's
78 auto decode = std::make_unique<unsigned char[]>(nbytes);
79 auto decode_len = compactor_decode(encode.get(), nbytes, decode.get());
80 EXPECT_EQ(decode_len, nbytes);
81 for (int i = 0; i < nbytes; i++)
82 ASSERT_EQ(decode[i], 0) << "i = " << i;
83
84 // assign the entire array to be all 1's, and test again
85 buf.assign(nbytes, 255);
86 encode_len = compactor_encode(buf.data(), nbytes, encode.get(), nbytes);
87 EXPECT_EQ(encode_len, compactor_comp_size(buf.data(), nbytes));
88 decode_len = compactor_decode(encode.get(), nbytes, decode.get());
89 EXPECT_EQ(decode_len, nbytes);
90 for (int i = 0; i < nbytes; i++)
91 ASSERT_EQ(decode[i], 255) << "i = " << i;
92 }
93
94 TEST(compactor, coding_mixed)
95 {
96 int nbytes = 64; // 16 ints
97 auto buf = std::vector<unsigned char>(nbytes, 0);
98 for (int i = 0; i < nbytes / 4; i++)
99 buf[i] = 255;
100 for (int i = nbytes / 4; i < nbytes / 2; i++)
101 buf[i] = i;
102 buf[nbytes - 1] = 255;
103
104 // encode `buf`
105 auto encode = std::make_unique<unsigned char[]>(nbytes);
106 auto encode_len = compactor_encode(buf.data(), nbytes, encode.get(), nbytes);
107 EXPECT_EQ(encode_len, compactor_comp_size(buf.data(), nbytes));
108
109 // decode and compare to the original
110 auto decode = std::make_unique<unsigned char[]>(nbytes);
111 auto decode_len = compactor_decode(encode.get(), nbytes, decode.get());
112 EXPECT_EQ(decode_len, nbytes);
113 for (int i = 0; i < nbytes; i++)
114 ASSERT_EQ(buf[i], decode[i]) << "i = " << i;
115 }
116
117 } // End of the namespace
118