modified: makefile
[GalaxyCodeBases.git] / c_cpp / etc / jellyfish / unit_tests / test_offsets_key_value.cc
blob9677e2730ac16a5818a9ae3f026eeb3e7632178f
1 #include <gtest/gtest.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <map>
5 #include <vector>
6 #include <iostream>
7 #include <algorithm>
8 #include <jellyfish/offsets_key_value.hpp>
10 #ifndef UINT64_C
11 #define UINT64_C(x) ((uint64_t)x)
12 #endif
14 using namespace jellyfish;
16 typedef struct {
17 uint_t key_len, val_len;
18 } offset_test_param;
20 class ComputeOffsetsTest : public ::testing::TestWithParam<offset_test_param>
22 public:
23 Offsets<uint64_t> offsets;
25 ComputeOffsetsTest() :
26 offsets(GetParam().key_len, GetParam().val_len, 15)
27 { }
29 ~ComputeOffsetsTest() { }
32 TEST_P(ComputeOffsetsTest, CheckCoherency) {
33 const Offsets<uint64_t>::offset_t *it = NULL, *pit = NULL;
34 const Offsets<uint64_t>::offset_t *lit = NULL, *lpit = NULL;
35 uint_t k_len = GetParam().key_len;
36 uint_t v_len = GetParam().val_len;
37 uint_t kv_len = k_len + v_len;
38 uint_t lk_len = 4;
39 uint_t lv_len = kv_len - lk_len;
40 uint_t i = 0;
41 uint64_t *w, *pw;
43 // /* Still missing:
44 // * - testing of value masks
45 // */
46 EXPECT_EQ(lk_len, this->offsets.get_reprobe_len());
47 EXPECT_EQ(lv_len, this->offsets.get_lval_len());
49 for(i = 0; true; i++) {
50 w = this->offsets.get_word_offset(i, &it, &lit, NULL);
51 if(i > 0) {
52 pw = this->offsets.get_word_offset(i-1, &pit, &lpit, NULL);
54 uint_t noff = pit->key.boff + kv_len;
55 if(noff > 64) {
56 EXPECT_EQ(pit->key.woff + 1, it->key.woff) <<
57 i << ": Word offset should go up";
58 } else {
59 EXPECT_EQ(pit->key.woff, it->key.woff) <<
60 i << ": Word offset should NOT go up";
63 if(pit->key.mask2) { // key between two words
64 EXPECT_LT((uint_t)64, pit->key.boff + k_len) <<
65 i << ": Key not between words, should not have mask2";
66 } else {
67 EXPECT_GE((uint_t)64, pit->key.boff - 1 + k_len + 1) <<
68 i << ": Key between words, should have mask2";
70 EXPECT_EQ((pit->key.boff + kv_len + 1 + (pit->key.mask2 ? 2 : 0)) % 64,
71 it->key.boff) <<
72 i << ": Invalid jump of bit offset";
73 EXPECT_EQ((pit->key.boff + kv_len + 1 + (pit->key.mask2 ? 2 : 0)) % 64,
74 lit->key.boff) <<
75 i << ": Invalid jump of bit offset large field";
76 } // if(i > 0)
78 EXPECT_EQ((it->key.boff + k_len + (it->key.mask2 ? 2 : 0)) % 64, it->val.boff) <<
79 i << ": Invalid value offset";
80 EXPECT_EQ((lit->key.boff + lk_len + (lit->key.mask2 ? 2 : 0)) % 64, lit->val.boff) <<
81 i << ": Invalid value offset large field";
82 EXPECT_EQ(UINT64_C(1) << (it->key.boff - 1), it->key.lb_mask) <<
83 i << ": Invalid lb_mask";
84 EXPECT_EQ(UINT64_C(1) << (lit->key.boff - 1), lit->key.lb_mask) <<
85 i << ": Invalid lb_mask";
86 if(it->key.mask2) {
87 EXPECT_EQ(64 - it->key.boff - 1, it->key.shift) <<
88 i << ": invalid key shift";
89 EXPECT_EQ(UINT64_C(1) << (64 - it->key.boff + 1), 1 + (it->key.mask1 >> (it->key.boff - 1))) <<
90 i << ": invalid key mask";
91 EXPECT_EQ(UINT64_C(1) << (it->key.boff + k_len - 64 + 2), 1 + it->key.mask2) <<
92 i << ": invalid key mask2";
93 EXPECT_EQ(k_len, it->key.shift + it->key.cshift);
94 EXPECT_EQ(UINT64_C(1) << 63, it->key.sb_mask1) <<
95 i << ": invalid key key sb_mask1";
96 EXPECT_EQ(UINT64_C(1) << it->key.cshift, it->key.sb_mask2) <<
97 i << ": invalid key sb_mask2";
98 } else {
99 EXPECT_EQ(0u, it->key.shift) <<
100 i << ": key shift should be zero, no mask";
101 EXPECT_EQ(UINT64_C(1) << (k_len + 1), 1 + (it->key.mask1 >> (it->key.boff - 1))) <<
102 i << ": invalid key mask " << it->key.boff;
103 EXPECT_EQ(0u, it->key.cshift);
104 EXPECT_EQ(0u, it->key.sb_mask1 | it->key.sb_mask2) <<
105 i << ": set bit masks should be 0";
107 if(lit->key.mask2) {
108 EXPECT_EQ(64 - lit->key.boff - 1, lit->key.shift) <<
109 i << ": invalid key shift large field";
110 EXPECT_EQ(UINT64_C(1) << (64 - lit->key.boff + 1), 1 + (lit->key.mask1 >> (lit->key.boff - 1))) <<
111 i << ": invalid key mask large field";
112 EXPECT_EQ(UINT64_C(1) << (lit->key.boff + lk_len - 64 + 2), 1 + lit->key.mask2) <<
113 i << ": invalid key mask2 large field";
114 EXPECT_EQ(lk_len, lit->key.shift + lit->key.cshift);
115 EXPECT_EQ(UINT64_C(1) << 63, lit->key.sb_mask1) <<
116 i << ": invalid key key sb_mask1";
117 EXPECT_EQ(UINT64_C(1) << lit->key.cshift, lit->key.sb_mask2) <<
118 i << ": invalid key sb_mask2";
119 } else {
120 EXPECT_EQ(0u, lit->key.shift) <<
121 i << ": key shift should be zero, no mask, large field";
122 EXPECT_EQ(0u, lit->key.cshift);
123 EXPECT_EQ(UINT64_C(1) << (lk_len + 1), 1 + (lit->key.mask1 >> (lit->key.boff - 1))) <<
124 i << ": invalid key mask large field " << lit->key.boff;
127 if(it->val.mask2) {
128 EXPECT_LT((uint_t)64, it->val.boff + v_len) <<
129 i << ": val not between words, should not have mask2";
130 EXPECT_EQ(64 - it->val.boff, it->val.shift) <<
131 i << ": invalid val shift";
132 EXPECT_EQ(UINT64_C(1) << (64 - it->val.boff), 1 + (it->val.mask1 >> it->val.boff)) <<
133 i << ": invalid val mask1";
134 EXPECT_EQ(UINT64_C(1) << (it->val.boff + v_len - 64), 1 + it->val.mask2) <<
135 i << ": invalid val mask2";
136 } else {
137 EXPECT_GE((uint_t)64, it->val.boff + v_len) <<
138 i << ": val between words, should have mask2";
139 EXPECT_EQ(v_len, it->val.shift) <<
140 i << ": invalid val shift";
141 EXPECT_EQ(UINT64_C(1) << v_len, 1 + (it->val.mask1 >> it->val.boff)) <<
142 i << ": invalid val mask1";
144 EXPECT_EQ(v_len, it->val.shift + it->val.cshift);
145 if(lit->val.mask2) {
146 EXPECT_LT((uint_t)64, lit->val.boff + lv_len) <<
147 i << ": val not between words, should not have mask2 large field";
148 EXPECT_EQ(64 - lit->val.boff, lit->val.shift) <<
149 i << ": invalid val shift large field";
150 EXPECT_EQ(UINT64_C(1) << (64 - lit->val.boff), 1 + (lit->val.mask1 >> lit->val.boff)) <<
151 i << ": invalid val mask1 large field";
152 EXPECT_EQ(UINT64_C(1) << (lit->val.boff + lv_len - 64), 1 + lit->val.mask2) <<
153 i << ": invalid val mask2 large field";
154 } else {
155 EXPECT_GE((uint_t)64, lit->val.boff + lv_len) <<
156 i << ": val between words, should have mask2 large field " << lit->val.boff << " " << lv_len;
157 EXPECT_EQ(lv_len, lit->val.shift) <<
158 i << ": invalid val shift large field";
159 EXPECT_EQ(UINT64_C(1) << lv_len, 1 + (lit->val.mask1 >> lit->val.boff)) <<
160 i << ": invalid val mask1 large field";
162 EXPECT_EQ(lv_len, lit->val.shift + lit->val.cshift);
164 uint_t noff = it->key.boff + kv_len;
165 if(noff == 62 || noff == 63 || noff == 64)
166 break;
170 offset_test_param params[] = { {10, 10}, { 22, 5 }, {20, 5} };
171 INSTANTIATE_TEST_CASE_P(OffsetsTest, ComputeOffsetsTest, ::testing::ValuesIn(params));