99TEST (JSONBinPack_Encoder, context_record_string) {
1010 sourcemeta::jsonbinpack::Context context;
1111 using ContextType = sourcemeta::jsonbinpack::Context::Type;
12- EXPECT_FALSE (context.has (" foo" , ContextType::Standalone));
12+ const auto result_1{context.find (" foo" , ContextType::Standalone)};
13+ EXPECT_FALSE (result_1.has_value ());
1314 context.record (" foo" , 2 , ContextType::Standalone);
14- EXPECT_TRUE (context.has (" foo" , ContextType::Standalone));
15- EXPECT_EQ (context.offset (" foo" , ContextType::Standalone), 2 );
15+ const auto result_2{context.find (" foo" , ContextType::Standalone)};
16+ EXPECT_TRUE (result_2.has_value ());
17+ EXPECT_EQ (result_2.value (), 2 );
1618}
1719
1820TEST (JSONBinPack_Encoder, context_record_string_too_short) {
1921 sourcemeta::jsonbinpack::Context context;
2022 using ContextType = sourcemeta::jsonbinpack::Context::Type;
21- EXPECT_FALSE (context.has (" fo" , ContextType::Standalone));
23+ const auto result_1{context.find (" fo" , ContextType::Standalone)};
24+ EXPECT_FALSE (result_1.has_value ());
2225 context.record (" fo" , 2 , ContextType::Standalone);
23- EXPECT_FALSE (context.has (" fo" , ContextType::Standalone));
26+ const auto result_2{context.find (" fo" , ContextType::Standalone)};
27+ EXPECT_FALSE (result_2.has_value ());
2428}
2529
2630TEST (JSONBinPack_Encoder, context_record_string_empty) {
2731 sourcemeta::jsonbinpack::Context context;
2832 using ContextType = sourcemeta::jsonbinpack::Context::Type;
29- EXPECT_FALSE (context.has (" " , ContextType::Standalone));
33+ const auto result_1{context.find (" fo" , ContextType::Standalone)};
34+ EXPECT_FALSE (result_1.has_value ());
3035 context.record (" " , 2 , ContextType::Standalone);
31- EXPECT_FALSE (context.has (" " , ContextType::Standalone));
36+ const auto result_2{context.find (" " , ContextType::Standalone)};
37+ EXPECT_FALSE (result_2.has_value ());
3238}
3339
3440TEST (JSONBinPack_Encoder, context_has_on_unknown_string) {
3541 sourcemeta::jsonbinpack::Context context;
3642 using ContextType = sourcemeta::jsonbinpack::Context::Type;
37- EXPECT_FALSE (context.has (" foobarbaz" , ContextType::Standalone));
43+ const auto result{context.find (" foobarbaz" , ContextType::Standalone)};
44+ EXPECT_FALSE (result.has_value ());
3845}
3946
4047TEST (JSONBinPack_Encoder, context_increase_offset) {
4148 sourcemeta::jsonbinpack::Context context;
4249 using ContextType = sourcemeta::jsonbinpack::Context::Type;
4350 context.record (" foo" , 2 , ContextType::Standalone);
4451 context.record (" foo" , 4 , ContextType::Standalone);
45- EXPECT_EQ (context.offset (" foo" , ContextType::Standalone), 4 );
52+ const auto result{context.find (" foo" , ContextType::Standalone)};
53+ EXPECT_TRUE (result.has_value ());
54+ EXPECT_EQ (result.value (), 4 );
4655}
4756
4857TEST (JSONBinPack_Encoder, context_do_not_decrease_offset) {
4958 sourcemeta::jsonbinpack::Context context;
5059 using ContextType = sourcemeta::jsonbinpack::Context::Type;
5160 context.record (" foo" , 4 , ContextType::Standalone);
5261 context.record (" foo" , 2 , ContextType::Standalone);
53- EXPECT_EQ (context.offset (" foo" , ContextType::Standalone), 4 );
62+ const auto result{context.find (" foo" , ContextType::Standalone)};
63+ EXPECT_TRUE (result.has_value ());
64+ EXPECT_EQ (result.value (), 4 );
5465}
5566
5667TEST (JSONBinPack_Encoder, context_not_record_too_big) {
@@ -60,7 +71,8 @@ TEST(JSONBinPack_Encoder, context_not_record_too_big) {
6071 sourcemeta::jsonbinpack::Context context;
6172 using ContextType = sourcemeta::jsonbinpack::Context::Type;
6273 context.record (too_big, 1 , ContextType::Standalone);
63- EXPECT_FALSE (context.has (too_big, ContextType::Standalone));
74+ const auto result{context.find (too_big, ContextType::Standalone)};
75+ EXPECT_FALSE (result.has_value ());
6476}
6577
6678TEST (JSONBinPack_Encoder, context_remove_oldest) {
@@ -70,27 +82,31 @@ TEST(JSONBinPack_Encoder, context_remove_oldest) {
7082 context.record (" bar" , 3 , ContextType::Standalone);
7183 context.record (" baz" , 7 , ContextType::PrefixLengthVarintPlusOne);
7284
73- EXPECT_TRUE (context.has (" foo" , ContextType::Standalone));
74- EXPECT_TRUE (context.has (" bar" , ContextType::Standalone));
75- EXPECT_TRUE (context.has (" baz" , ContextType::PrefixLengthVarintPlusOne));
85+ EXPECT_TRUE (context.find (" foo" , ContextType::Standalone).has_value ());
86+ EXPECT_TRUE (context.find (" bar" , ContextType::Standalone).has_value ());
87+ EXPECT_TRUE (
88+ context.find (" baz" , ContextType::PrefixLengthVarintPlusOne).has_value ());
7689
7790 context.remove_oldest ();
7891
79- EXPECT_TRUE (context.has (" foo" , ContextType::Standalone));
80- EXPECT_FALSE (context.has (" bar" , ContextType::Standalone));
81- EXPECT_TRUE (context.has (" baz" , ContextType::PrefixLengthVarintPlusOne));
92+ EXPECT_TRUE (context.find (" foo" , ContextType::Standalone).has_value ());
93+ EXPECT_FALSE (context.find (" bar" , ContextType::Standalone).has_value ());
94+ EXPECT_TRUE (
95+ context.find (" baz" , ContextType::PrefixLengthVarintPlusOne).has_value ());
8296
8397 context.remove_oldest ();
8498
85- EXPECT_TRUE (context.has (" foo" , ContextType::Standalone));
86- EXPECT_FALSE (context.has (" bar" , ContextType::Standalone));
87- EXPECT_FALSE (context.has (" baz" , ContextType::PrefixLengthVarintPlusOne));
99+ EXPECT_TRUE (context.find (" foo" , ContextType::Standalone).has_value ());
100+ EXPECT_FALSE (context.find (" bar" , ContextType::Standalone).has_value ());
101+ EXPECT_FALSE (
102+ context.find (" baz" , ContextType::PrefixLengthVarintPlusOne).has_value ());
88103
89104 context.remove_oldest ();
90105
91- EXPECT_FALSE (context.has (" foo" , ContextType::Standalone));
92- EXPECT_FALSE (context.has (" bar" , ContextType::Standalone));
93- EXPECT_FALSE (context.has (" baz" , ContextType::PrefixLengthVarintPlusOne));
106+ EXPECT_FALSE (context.find (" foo" , ContextType::Standalone).has_value ());
107+ EXPECT_FALSE (context.find (" bar" , ContextType::Standalone).has_value ());
108+ EXPECT_FALSE (
109+ context.find (" baz" , ContextType::PrefixLengthVarintPlusOne).has_value ());
94110}
95111
96112TEST (JSONBinPack_Encoder, context_is_a_circular_buffer) {
@@ -117,27 +133,27 @@ TEST(JSONBinPack_Encoder, context_is_a_circular_buffer) {
117133 context.record (string_3, length * 2 , ContextType::Standalone);
118134 context.record (string_4, length * 3 , ContextType::Standalone);
119135
120- EXPECT_TRUE (context.has (string_1, ContextType::Standalone));
121- EXPECT_TRUE (context.has (string_2, ContextType::Standalone));
122- EXPECT_TRUE (context.has (string_3, ContextType::Standalone));
123- EXPECT_TRUE (context.has (string_4, ContextType::Standalone));
136+ EXPECT_TRUE (context.find (string_1, ContextType::Standalone). has_value ( ));
137+ EXPECT_TRUE (context.find (string_2, ContextType::Standalone). has_value ( ));
138+ EXPECT_TRUE (context.find (string_3, ContextType::Standalone). has_value ( ));
139+ EXPECT_TRUE (context.find (string_4, ContextType::Standalone). has_value ( ));
124140
125141 context.record (string_5, length * 4 , ContextType::Standalone);
126142
127- EXPECT_FALSE (context.has (string_1, ContextType::Standalone));
128- EXPECT_TRUE (context.has (string_2, ContextType::Standalone));
129- EXPECT_TRUE (context.has (string_3, ContextType::Standalone));
130- EXPECT_TRUE (context.has (string_4, ContextType::Standalone));
131- EXPECT_TRUE (context.has (string_5, ContextType::Standalone));
143+ EXPECT_FALSE (context.find (string_1, ContextType::Standalone). has_value ( ));
144+ EXPECT_TRUE (context.find (string_2, ContextType::Standalone). has_value ( ));
145+ EXPECT_TRUE (context.find (string_3, ContextType::Standalone). has_value ( ));
146+ EXPECT_TRUE (context.find (string_4, ContextType::Standalone). has_value ( ));
147+ EXPECT_TRUE (context.find (string_5, ContextType::Standalone). has_value ( ));
132148
133149 context.record (string_6, length * 5 , ContextType::Standalone);
134150
135- EXPECT_FALSE (context.has (string_1, ContextType::Standalone));
136- EXPECT_FALSE (context.has (string_2, ContextType::Standalone));
137- EXPECT_TRUE (context.has (string_3, ContextType::Standalone));
138- EXPECT_TRUE (context.has (string_4, ContextType::Standalone));
139- EXPECT_TRUE (context.has (string_5, ContextType::Standalone));
140- EXPECT_TRUE (context.has (string_6, ContextType::Standalone));
151+ EXPECT_FALSE (context.find (string_1, ContextType::Standalone). has_value ( ));
152+ EXPECT_FALSE (context.find (string_2, ContextType::Standalone). has_value ( ));
153+ EXPECT_TRUE (context.find (string_3, ContextType::Standalone). has_value ( ));
154+ EXPECT_TRUE (context.find (string_4, ContextType::Standalone). has_value ( ));
155+ EXPECT_TRUE (context.find (string_5, ContextType::Standalone). has_value ( ));
156+ EXPECT_TRUE (context.find (string_6, ContextType::Standalone). has_value ( ));
141157}
142158
143159TEST (JSONBinPack_Encoder, context_same_string_different_type) {
@@ -146,17 +162,22 @@ TEST(JSONBinPack_Encoder, context_same_string_different_type) {
146162 context.record (" foo" , 10 , ContextType::Standalone);
147163 context.record (" foo" , 20 , ContextType::PrefixLengthVarintPlusOne);
148164
149- EXPECT_TRUE (context.has (" foo" , ContextType::Standalone));
150- EXPECT_TRUE (context.has (" foo" , ContextType::PrefixLengthVarintPlusOne));
165+ const auto result_1{context.find (" foo" , ContextType::Standalone)};
166+ const auto result_2{
167+ context.find (" foo" , ContextType::PrefixLengthVarintPlusOne)};
151168
152- EXPECT_EQ (context.offset (" foo" , ContextType::Standalone), 10 );
153- EXPECT_EQ (context.offset (" foo" , ContextType::PrefixLengthVarintPlusOne), 20 );
169+ EXPECT_TRUE (result_1.has_value ());
170+ EXPECT_TRUE (result_2.has_value ());
171+
172+ EXPECT_EQ (result_1.value (), 10 );
173+ EXPECT_EQ (result_2.value (), 20 );
154174}
155175
156176TEST (JSONBinPack_Encoder, context_no_fallback_type) {
157177 sourcemeta::jsonbinpack::Context context;
158178 using ContextType = sourcemeta::jsonbinpack::Context::Type;
159179 context.record (" foo" , 10 , ContextType::Standalone);
160- EXPECT_TRUE (context.has (" foo" , ContextType::Standalone));
161- EXPECT_FALSE (context.has (" foo" , ContextType::PrefixLengthVarintPlusOne));
180+ EXPECT_TRUE (context.find (" foo" , ContextType::Standalone).has_value ());
181+ EXPECT_FALSE (
182+ context.find (" foo" , ContextType::PrefixLengthVarintPlusOne).has_value ());
162183}
0 commit comments