Debian Codegraph @

src/kj/common-test.c++

raw
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #include "common.h"
23 #include "test.h"
24 #include <inttypes.h>
25 #include <kj/compat/gtest.h>
26
27 namespace kj {
28 namespace {
29
30 KJ_ASSERT_CAN_MEMCPY(char);
31 KJ_ASSERT_CAN_MEMCPY(byte);
32
33 KJ_TEST("kj::size() on native arrays") {
34 int arr[] = {12, 34, 56, 78};
35
36 size_t expected = 0;
37 for (size_t i: indices(arr)) {
38 KJ_EXPECT(i == expected++);
39 }
40 KJ_EXPECT(expected == 4u);
41 }
42
43 struct ImplicitToInt {
44 int i;
45
46 operator int() const {
47 return i;
48 }
49 };
50
51 struct Immovable {
52 Immovable() = default;
53 KJ_DISALLOW_COPY_AND_MOVE(Immovable);
54 };
55
56 struct CopyOrMove {
57 // Type that detects the difference between copy and move.
58 CopyOrMove(int i): i(i) {}
59 CopyOrMove(CopyOrMove&& other): i(other.i) { other.i = -1; }
60 CopyOrMove(const CopyOrMove&) = default;
61
62 int i;
63 };
64
65 TEST(Common, Maybe) {
66 {
67 Maybe<int> m = 123;
68 EXPECT_FALSE(m == nullptr);
69 EXPECT_TRUE(m != nullptr);
70 KJ_IF_MAYBE(v, m) {
71 EXPECT_EQ(123, *v);
72 } else {
73 ADD_FAILURE();
74 }
75 KJ_IF_MAYBE(v, mv(m)) {
76 EXPECT_EQ(123, *v);
77 } else {
78 ADD_FAILURE();
79 }
80 EXPECT_EQ(123, m.orDefault(456));
81 bool ranLazy = false;
82 EXPECT_EQ(123, m.orDefault([&] {
83 ranLazy = true;
84 return 456;
85 }));
86 EXPECT_FALSE(ranLazy);
87
88 KJ_IF_MAYBE(v, m) {
89 int notUsedForRef = 5;
90 const int& ref = m.orDefault([&]() -> int& { return notUsedForRef; });
91
92 EXPECT_EQ(ref, *v);
93 EXPECT_EQ(&ref, v);
94
95 const int& ref2 = m.orDefault([notUsed = 5]() -> int { return notUsed; });
96 EXPECT_NE(&ref, &ref2);
97 EXPECT_EQ(ref2, 123);
98 } else {
99 ADD_FAILURE();
100 }
101 }
102
103 {
104 Maybe<Own<CopyOrMove>> m = kj::heap<CopyOrMove>(123);
105 EXPECT_FALSE(m == nullptr);
106 EXPECT_TRUE(m != nullptr);
107 KJ_IF_MAYBE(v, m) {
108 EXPECT_EQ(123, (*v)->i);
109 } else {
110 ADD_FAILURE();
111 }
112 KJ_IF_MAYBE(v, mv(m)) {
113 EXPECT_EQ(123, (*v)->i);
114 } else {
115 ADD_FAILURE();
116 }
117 // We have moved the kj::Own away, so this should give us the default and leave the Maybe empty.
118 EXPECT_EQ(456, m.orDefault(heap<CopyOrMove>(456))->i);
119 EXPECT_TRUE(m == nullptr);
120
121 bool ranLazy = false;
122 EXPECT_EQ(123, mv(m).orDefault([&] {
123 ranLazy = true;
124 return heap<CopyOrMove>(123);
125 })->i);
126 EXPECT_TRUE(ranLazy);
127 EXPECT_TRUE(m == nullptr);
128
129 m = heap<CopyOrMove>(123);
130 EXPECT_TRUE(m != nullptr);
131 ranLazy = false;
132 EXPECT_EQ(123, mv(m).orDefault([&] {
133 ranLazy = true;
134 return heap<CopyOrMove>(456);
135 })->i);
136 EXPECT_FALSE(ranLazy);
137 EXPECT_TRUE(m == nullptr);
138 }
139
140 {
141 Maybe<int> empty;
142 int defaultValue = 5;
143 auto& ref1 = empty.orDefault([&defaultValue]() -> int& {
144 return defaultValue;
145 });
146 EXPECT_EQ(&ref1, &defaultValue);
147
148 auto ref2 = empty.orDefault([&]() -> int { return defaultValue; });
149 EXPECT_NE(&ref2, &defaultValue);
150 }
151
152 {
153 Maybe<int> m = 0;
154 EXPECT_FALSE(m == nullptr);
155 EXPECT_TRUE(m != nullptr);
156 KJ_IF_MAYBE(v, m) {
157 EXPECT_EQ(0, *v);
158 } else {
159 ADD_FAILURE();
160 }
161 KJ_IF_MAYBE(v, mv(m)) {
162 EXPECT_EQ(0, *v);
163 } else {
164 ADD_FAILURE();
165 }
166 EXPECT_EQ(0, m.orDefault(456));
167 bool ranLazy = false;
168 EXPECT_EQ(0, m.orDefault([&] {
169 ranLazy = true;
170 return 456;
171 }));
172 EXPECT_FALSE(ranLazy);
173 }
174
175 {
176 Maybe<int> m = nullptr;
177 EXPECT_TRUE(m == nullptr);
178 EXPECT_FALSE(m != nullptr);
179 KJ_IF_MAYBE(v, m) {
180 ADD_FAILURE();
181 EXPECT_EQ(0, *v); // avoid unused warning
182 }
183 KJ_IF_MAYBE(v, mv(m)) {
184 ADD_FAILURE();
185 EXPECT_EQ(0, *v); // avoid unused warning
186 }
187 EXPECT_EQ(456, m.orDefault(456));
188 bool ranLazy = false;
189 EXPECT_EQ(456, m.orDefault([&] {
190 ranLazy = true;
191 return 456;
192 }));
193 EXPECT_TRUE(ranLazy);
194 }
195
196 int i = 234;
197 {
198 Maybe<int&> m = i;
199 EXPECT_FALSE(m == nullptr);
200 EXPECT_TRUE(m != nullptr);
201 KJ_IF_MAYBE(v, m) {
202 EXPECT_EQ(&i, v);
203 } else {
204 ADD_FAILURE();
205 }
206 KJ_IF_MAYBE(v, mv(m)) {
207 EXPECT_EQ(&i, v);
208 } else {
209 ADD_FAILURE();
210 }
211 EXPECT_EQ(234, m.orDefault(456));
212 }
213
214 {
215 Maybe<int&> m = nullptr;
216 EXPECT_TRUE(m == nullptr);
217 EXPECT_FALSE(m != nullptr);
218 KJ_IF_MAYBE(v, m) {
219 ADD_FAILURE();
220 EXPECT_EQ(0, *v); // avoid unused warning
221 }
222 KJ_IF_MAYBE(v, mv(m)) {
223 ADD_FAILURE();
224 EXPECT_EQ(0, *v); // avoid unused warning
225 }
226 EXPECT_EQ(456, m.orDefault(456));
227 }
228
229 {
230 Maybe<int&> m = &i;
231 EXPECT_FALSE(m == nullptr);
232 EXPECT_TRUE(m != nullptr);
233 KJ_IF_MAYBE(v, m) {
234 EXPECT_EQ(&i, v);
235 } else {
236 ADD_FAILURE();
237 }
238 KJ_IF_MAYBE(v, mv(m)) {
239 EXPECT_EQ(&i, v);
240 } else {
241 ADD_FAILURE();
242 }
243 EXPECT_EQ(234, m.orDefault(456));
244 }
245
246 {
247 const Maybe<int&> m2 = &i;
248 Maybe<const int&> m = m2;
249 EXPECT_FALSE(m == nullptr);
250 EXPECT_TRUE(m != nullptr);
251 KJ_IF_MAYBE(v, m) {
252 EXPECT_EQ(&i, v);
253 } else {
254 ADD_FAILURE();
255 }
256 KJ_IF_MAYBE(v, mv(m)) {
257 EXPECT_EQ(&i, v);
258 } else {
259 ADD_FAILURE();
260 }
261 EXPECT_EQ(234, m.orDefault(456));
262 }
263
264 {
265 Maybe<int&> m = implicitCast<int*>(nullptr);
266 EXPECT_TRUE(m == nullptr);
267 EXPECT_FALSE(m != nullptr);
268 KJ_IF_MAYBE(v, m) {
269 ADD_FAILURE();
270 EXPECT_EQ(0, *v); // avoid unused warning
271 }
272 KJ_IF_MAYBE(v, mv(m)) {
273 ADD_FAILURE();
274 EXPECT_EQ(0, *v); // avoid unused warning
275 }
276 EXPECT_EQ(456, m.orDefault(456));
277 }
278
279 {
280 Maybe<int> mi = i;
281 Maybe<int&> m = mi;
282 EXPECT_FALSE(m == nullptr);
283 EXPECT_TRUE(m != nullptr);
284 KJ_IF_MAYBE(v, m) {
285 EXPECT_EQ(&KJ_ASSERT_NONNULL(mi), v);
286 } else {
287 ADD_FAILURE();
288 }
289 KJ_IF_MAYBE(v, mv(m)) {
290 EXPECT_EQ(&KJ_ASSERT_NONNULL(mi), v);
291 } else {
292 ADD_FAILURE();
293 }
294 EXPECT_EQ(234, m.orDefault(456));
295 }
296
297 {
298 Maybe<int> mi = nullptr;
299 Maybe<int&> m = mi;
300 EXPECT_TRUE(m == nullptr);
301 KJ_IF_MAYBE(v, m) {
302 KJ_FAIL_EXPECT(*v);
303 }
304 }
305
306 {
307 const Maybe<int> mi = i;
308 Maybe<const int&> m = mi;
309 EXPECT_FALSE(m == nullptr);
310 EXPECT_TRUE(m != nullptr);
311 KJ_IF_MAYBE(v, m) {
312 EXPECT_EQ(&KJ_ASSERT_NONNULL(mi), v);
313 } else {
314 ADD_FAILURE();
315 }
316 KJ_IF_MAYBE(v, mv(m)) {
317 EXPECT_EQ(&KJ_ASSERT_NONNULL(mi), v);
318 } else {
319 ADD_FAILURE();
320 }
321 EXPECT_EQ(234, m.orDefault(456));
322 }
323
324 {
325 const Maybe<int> mi = nullptr;
326 Maybe<const int&> m = mi;
327 EXPECT_TRUE(m == nullptr);
328 KJ_IF_MAYBE(v, m) {
329 KJ_FAIL_EXPECT(*v);
330 }
331 }
332
333 {
334 // Verify orDefault() works with move-only types.
335 Maybe<kj::String> m = nullptr;
336 kj::String s = kj::mv(m).orDefault(kj::str("foo"));
337 EXPECT_EQ("foo", s);
338 EXPECT_EQ("foo", kj::mv(m).orDefault([] {
339 return kj::str("foo");
340 }));
341 }
342
343 {
344 // Test a case where an implicit conversion didn't used to happen correctly.
345 Maybe<ImplicitToInt> m(ImplicitToInt { 123 });
346 Maybe<uint> m2(m);
347 Maybe<uint> m3(kj::mv(m));
348 KJ_IF_MAYBE(v, m2) {
349 EXPECT_EQ(123, *v);
350 } else {
351 ADD_FAILURE();
352 }
353 KJ_IF_MAYBE(v, m3) {
354 EXPECT_EQ(123, *v);
355 } else {
356 ADD_FAILURE();
357 }
358 }
359
360 {
361 // Test usage of immovable types.
362 Maybe<Immovable> m;
363 KJ_EXPECT(m == nullptr);
364 m.emplace();
365 KJ_EXPECT(m != nullptr);
366 m = nullptr;
367 KJ_EXPECT(m == nullptr);
368 }
369
370 {
371 // Test that initializing Maybe<T> from Maybe<T&>&& does a copy, not a move.
372 CopyOrMove x(123);
373 Maybe<CopyOrMove&> m(x);
374 Maybe<CopyOrMove> m2 = kj::mv(m);
375 KJ_EXPECT(m == nullptr); // m is moved out of and cleared
376 KJ_EXPECT(x.i == 123); // but what m *referenced* was not moved out of
377 KJ_EXPECT(KJ_ASSERT_NONNULL(m2).i == 123); // m2 is a copy of what m referenced
378 }
379
380 {
381 // Test that a moved-out-of Maybe<T> is left empty after move constructor.
382 Maybe<int> m = 123;
383 KJ_EXPECT(m != nullptr);
384
385 Maybe<int> n(kj::mv(m));
386 KJ_EXPECT(m == nullptr);
387 KJ_EXPECT(n != nullptr);
388 }
389
390 {
391 // Test that a moved-out-of Maybe<T> is left empty after move constructor.
392 Maybe<int> m = 123;
393 KJ_EXPECT(m != nullptr);
394
395 Maybe<int> n = kj::mv(m);
396 KJ_EXPECT(m == nullptr);
397 KJ_EXPECT(n != nullptr);
398 }
399
400 {
401 // Test that a moved-out-of Maybe<T&> is left empty when moved to a Maybe<T>.
402 int x = 123;
403 Maybe<int&> m = x;
404 KJ_EXPECT(m != nullptr);
405
406 Maybe<int> n(kj::mv(m));
407 KJ_EXPECT(m == nullptr);
408 KJ_EXPECT(n != nullptr);
409 }
410
411 {
412 // Test that a moved-out-of Maybe<T&> is left empty when moved to another Maybe<T&>.
413 int x = 123;
414 Maybe<int&> m = x;
415 KJ_EXPECT(m != nullptr);
416
417 Maybe<int&> n(kj::mv(m));
418 KJ_EXPECT(m == nullptr);
419 KJ_EXPECT(n != nullptr);
420 }
421
422 {
423 Maybe<int> m1 = 123;
424 Maybe<int> m2 = 123;
425 Maybe<int> m3 = 456;
426 Maybe<int> m4 = nullptr;
427 Maybe<int> m5 = nullptr;
428
429 KJ_EXPECT(m1 == m2);
430 KJ_EXPECT(m1 != m3);
431 KJ_EXPECT(m1 != m4);
432 KJ_EXPECT(m4 == m5);
433 KJ_EXPECT(m4 != m1);
434 }
435 }
436
437 TEST(Common, MaybeConstness) {
438 int i;
439
440 Maybe<int&> mi = i;
441 const Maybe<int&> cmi = mi;
442 // const Maybe<int&> cmi2 = cmi; // shouldn't compile! Transitive const violation.
443
444 KJ_IF_MAYBE(i2, cmi) {
445 EXPECT_EQ(&i, i2);
446 } else {
447 ADD_FAILURE();
448 }
449
450 Maybe<const int&> mci = mi;
451 const Maybe<const int&> cmci = mci;
452 const Maybe<const int&> cmci2 = cmci;
453
454 KJ_IF_MAYBE(i2, cmci2) {
455 EXPECT_EQ(&i, i2);
456 } else {
457 ADD_FAILURE();
458 }
459 }
460
461 #if __GNUC__
462 TEST(Common, MaybeUnwrapOrReturn) {
463 {
464 auto func = [](Maybe<int> i) -> int {
465 int& j = KJ_UNWRAP_OR_RETURN(i, -1);
466 KJ_EXPECT(&j == &KJ_ASSERT_NONNULL(i));
467 return j + 2;
468 };
469
470 KJ_EXPECT(func(123) == 125);
471 KJ_EXPECT(func(nullptr) == -1);
472 }
473
474 {
475 auto func = [&](Maybe<String> maybe) -> int {
476 String str = KJ_UNWRAP_OR_RETURN(kj::mv(maybe), -1);
477 return str.parseAs<int>();
478 };
479
480 KJ_EXPECT(func(kj::str("123")) == 123);
481 KJ_EXPECT(func(nullptr) == -1);
482 }
483
484 // Test void return.
485 {
486 int val = 0;
487 auto func = [&](Maybe<int> i) {
488 val = KJ_UNWRAP_OR_RETURN(i);
489 };
490
491 func(123);
492 KJ_EXPECT(val == 123);
493 val = 321;
494 func(nullptr);
495 KJ_EXPECT(val == 321);
496 }
497
498 // Test KJ_UNWRAP_OR
499 {
500 bool wasNull = false;
501 auto func = [&](Maybe<int> i) -> int {
502 int& j = KJ_UNWRAP_OR(i, {
503 wasNull = true;
504 return -1;
505 });
506 KJ_EXPECT(&j == &KJ_ASSERT_NONNULL(i));
507 return j + 2;
508 };
509
510 KJ_EXPECT(func(123) == 125);
511 KJ_EXPECT(!wasNull);
512 KJ_EXPECT(func(nullptr) == -1);
513 KJ_EXPECT(wasNull);
514 }
515
516 {
517 bool wasNull = false;
518 auto func = [&](Maybe<String> maybe) -> int {
519 String str = KJ_UNWRAP_OR(kj::mv(maybe), {
520 wasNull = true;
521 return -1;
522 });
523 return str.parseAs<int>();
524 };
525
526 KJ_EXPECT(func(kj::str("123")) == 123);
527 KJ_EXPECT(!wasNull);
528 KJ_EXPECT(func(nullptr) == -1);
529 KJ_EXPECT(wasNull);
530 }
531
532 // Test void return.
533 {
534 int val = 0;
535 auto func = [&](Maybe<int> i) {
536 val = KJ_UNWRAP_OR(i, {
537 return;
538 });
539 };
540
541 func(123);
542 KJ_EXPECT(val == 123);
543 val = 321;
544 func(nullptr);
545 KJ_EXPECT(val == 321);
546 }
547
548 }
549 #endif
550
551 class Foo {
552 public:
553 KJ_DISALLOW_COPY_AND_MOVE(Foo);
554 virtual ~Foo() {}
555 protected:
556 Foo() = default;
557 };
558
559 class Bar: public Foo {
560 public:
561 Bar() = default;
562 KJ_DISALLOW_COPY_AND_MOVE(Bar);
563 virtual ~Bar() {}
564 };
565
566 class Baz: public Foo {
567 public:
568 Baz() = delete;
569 KJ_DISALLOW_COPY_AND_MOVE(Baz);
570 virtual ~Baz() {}
571 };
572
573 TEST(Common, Downcast) {
574 Bar bar;
575 Foo& foo = bar;
576
577 EXPECT_EQ(&bar, &downcast<Bar>(foo));
578 #if defined(KJ_DEBUG) && !KJ_NO_RTTI
579 KJ_EXPECT_THROW_MESSAGE("Value cannot be downcast", downcast<Baz>(foo));
580 #endif
581
582 #if KJ_NO_RTTI
583 EXPECT_TRUE(dynamicDowncastIfAvailable<Bar>(foo) == nullptr);
584 EXPECT_TRUE(dynamicDowncastIfAvailable<Baz>(foo) == nullptr);
585 #else
586 KJ_IF_MAYBE(m, dynamicDowncastIfAvailable<Bar>(foo)) {
587 EXPECT_EQ(&bar, m);
588 } else {
589 KJ_FAIL_ASSERT("Dynamic downcast returned null.");
590 }
591 EXPECT_TRUE(dynamicDowncastIfAvailable<Baz>(foo) == nullptr);
592 #endif
593 }
594
595 TEST(Common, MinMax) {
596 EXPECT_EQ(5, kj::min(5, 9));
597 EXPECT_EQ(5, kj::min(9, 5));
598 EXPECT_EQ(5, kj::min(5, 5));
599 EXPECT_EQ(9, kj::max(5, 9));
600 EXPECT_EQ(9, kj::max(9, 5));
601 EXPECT_EQ(5, kj::min(5, 5));
602
603 // Hey look, we can handle the types mismatching. Eat your heart out, std.
604 EXPECT_EQ(5, kj::min(5, 'a'));
605 EXPECT_EQ(5, kj::min('a', 5));
606 EXPECT_EQ('a', kj::max(5, 'a'));
607 EXPECT_EQ('a', kj::max('a', 5));
608
609 EXPECT_EQ('a', kj::min(1234567890123456789ll, 'a'));
610 EXPECT_EQ('a', kj::min('a', 1234567890123456789ll));
611 EXPECT_EQ(1234567890123456789ll, kj::max(1234567890123456789ll, 'a'));
612 EXPECT_EQ(1234567890123456789ll, kj::max('a', 1234567890123456789ll));
613 }
614
615 TEST(Common, MinMaxValue) {
616 EXPECT_EQ(0x7f, int8_t(maxValue));
617 EXPECT_EQ(0xffu, uint8_t(maxValue));
618 EXPECT_EQ(0x7fff, int16_t(maxValue));
619 EXPECT_EQ(0xffffu, uint16_t(maxValue));
620 EXPECT_EQ(0x7fffffff, int32_t(maxValue));
621 EXPECT_EQ(0xffffffffu, uint32_t(maxValue));
622 EXPECT_EQ(0x7fffffffffffffffll, int64_t(maxValue));
623 EXPECT_EQ(0xffffffffffffffffull, uint64_t(maxValue));
624
625 EXPECT_EQ(-0x80, int8_t(minValue));
626 EXPECT_EQ(0, uint8_t(minValue));
627 EXPECT_EQ(-0x8000, int16_t(minValue));
628 EXPECT_EQ(0, uint16_t(minValue));
629 EXPECT_EQ(-0x80000000, int32_t(minValue));
630 EXPECT_EQ(0, uint32_t(minValue));
631 EXPECT_EQ(-0x8000000000000000ll, int64_t(minValue));
632 EXPECT_EQ(0, uint64_t(minValue));
633
634 double f = inf();
635 EXPECT_TRUE(f * 2 == f);
636
637 f = nan();
638 EXPECT_FALSE(f == f);
639
640 // `char`'s signedness is platform-specific.
641 EXPECT_LE(char(minValue), '\0');
642 EXPECT_GE(char(maxValue), '\x7f');
643 }
644
645 TEST(Common, Defer) {
646 uint i = 0;
647 uint j = 1;
648 bool k = false;
649
650 {
651 KJ_DEFER(++i);
652 KJ_DEFER(j += 3; k = true);
653 EXPECT_EQ(0u, i);
654 EXPECT_EQ(1u, j);
655 EXPECT_FALSE(k);
656 }
657
658 EXPECT_EQ(1u, i);
659 EXPECT_EQ(4u, j);
660 EXPECT_TRUE(k);
661 }
662
663 TEST(Common, CanConvert) {
664 static_assert(canConvert<long, int>(), "failure");
665 static_assert(!canConvert<long, void*>(), "failure");
666
667 struct Super {};
668 struct Sub: public Super {};
669
670 static_assert(canConvert<Sub, Super>(), "failure");
671 static_assert(!canConvert<Super, Sub>(), "failure");
672 static_assert(canConvert<Sub*, Super*>(), "failure");
673 static_assert(!canConvert<Super*, Sub*>(), "failure");
674
675 static_assert(canConvert<void*, const void*>(), "failure");
676 static_assert(!canConvert<const void*, void*>(), "failure");
677 }
678
679 TEST(Common, ArrayAsBytes) {
680 uint32_t raw[] = { 0x12345678u, 0x9abcdef0u };
681
682 ArrayPtr<uint32_t> array = raw;
683 ASSERT_EQ(2, array.size());
684 EXPECT_EQ(0x12345678u, array[0]);
685 EXPECT_EQ(0x9abcdef0u, array[1]);
686
687 {
688 ArrayPtr<byte> bytes = array.asBytes();
689 ASSERT_EQ(8, bytes.size());
690
691 if (bytes[0] == '\x12') {
692 // big-endian
693 EXPECT_EQ(0x12u, bytes[0]);
694 EXPECT_EQ(0x34u, bytes[1]);
695 EXPECT_EQ(0x56u, bytes[2]);
696 EXPECT_EQ(0x78u, bytes[3]);
697 EXPECT_EQ(0x9au, bytes[4]);
698 EXPECT_EQ(0xbcu, bytes[5]);
699 EXPECT_EQ(0xdeu, bytes[6]);
700 EXPECT_EQ(0xf0u, bytes[7]);
701 } else {
702 // little-endian
703 EXPECT_EQ(0x12u, bytes[3]);
704 EXPECT_EQ(0x34u, bytes[2]);
705 EXPECT_EQ(0x56u, bytes[1]);
706 EXPECT_EQ(0x78u, bytes[0]);
707 EXPECT_EQ(0x9au, bytes[7]);
708 EXPECT_EQ(0xbcu, bytes[6]);
709 EXPECT_EQ(0xdeu, bytes[5]);
710 EXPECT_EQ(0xf0u, bytes[4]);
711 }
712 }
713
714 {
715 ArrayPtr<char> chars = array.asChars();
716 ASSERT_EQ(8, chars.size());
717
718 if (chars[0] == '\x12') {
719 // big-endian
720 EXPECT_EQ('\x12', chars[0]);
721 EXPECT_EQ('\x34', chars[1]);
722 EXPECT_EQ('\x56', chars[2]);
723 EXPECT_EQ('\x78', chars[3]);
724 EXPECT_EQ('\x9a', chars[4]);
725 EXPECT_EQ('\xbc', chars[5]);
726 EXPECT_EQ('\xde', chars[6]);
727 EXPECT_EQ('\xf0', chars[7]);
728 } else {
729 // little-endian
730 EXPECT_EQ('\x12', chars[3]);
731 EXPECT_EQ('\x34', chars[2]);
732 EXPECT_EQ('\x56', chars[1]);
733 EXPECT_EQ('\x78', chars[0]);
734 EXPECT_EQ('\x9a', chars[7]);
735 EXPECT_EQ('\xbc', chars[6]);
736 EXPECT_EQ('\xde', chars[5]);
737 EXPECT_EQ('\xf0', chars[4]);
738 }
739 }
740
741 ArrayPtr<const uint32_t> constArray = array;
742
743 {
744 ArrayPtr<const byte> bytes = constArray.asBytes();
745 ASSERT_EQ(8, bytes.size());
746
747 if (bytes[0] == '\x12') {
748 // big-endian
749 EXPECT_EQ(0x12u, bytes[0]);
750 EXPECT_EQ(0x34u, bytes[1]);
751 EXPECT_EQ(0x56u, bytes[2]);
752 EXPECT_EQ(0x78u, bytes[3]);
753 EXPECT_EQ(0x9au, bytes[4]);
754 EXPECT_EQ(0xbcu, bytes[5]);
755 EXPECT_EQ(0xdeu, bytes[6]);
756 EXPECT_EQ(0xf0u, bytes[7]);
757 } else {
758 // little-endian
759 EXPECT_EQ(0x12u, bytes[3]);
760 EXPECT_EQ(0x34u, bytes[2]);
761 EXPECT_EQ(0x56u, bytes[1]);
762 EXPECT_EQ(0x78u, bytes[0]);
763 EXPECT_EQ(0x9au, bytes[7]);
764 EXPECT_EQ(0xbcu, bytes[6]);
765 EXPECT_EQ(0xdeu, bytes[5]);
766 EXPECT_EQ(0xf0u, bytes[4]);
767 }
768 }
769
770 {
771 ArrayPtr<const char> chars = constArray.asChars();
772 ASSERT_EQ(8, chars.size());
773
774 if (chars[0] == '\x12') {
775 // big-endian
776 EXPECT_EQ('\x12', chars[0]);
777 EXPECT_EQ('\x34', chars[1]);
778 EXPECT_EQ('\x56', chars[2]);
779 EXPECT_EQ('\x78', chars[3]);
780 EXPECT_EQ('\x9a', chars[4]);
781 EXPECT_EQ('\xbc', chars[5]);
782 EXPECT_EQ('\xde', chars[6]);
783 EXPECT_EQ('\xf0', chars[7]);
784 } else {
785 // little-endian
786 EXPECT_EQ('\x12', chars[3]);
787 EXPECT_EQ('\x34', chars[2]);
788 EXPECT_EQ('\x56', chars[1]);
789 EXPECT_EQ('\x78', chars[0]);
790 EXPECT_EQ('\x9a', chars[7]);
791 EXPECT_EQ('\xbc', chars[6]);
792 EXPECT_EQ('\xde', chars[5]);
793 EXPECT_EQ('\xf0', chars[4]);
794 }
795 }
796 }
797
798 KJ_TEST("ArrayPtr operator ==") {
799 KJ_EXPECT(ArrayPtr<const int>({123, 456}) == ArrayPtr<const int>({123, 456}));
800 KJ_EXPECT(!(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123, 456})));
801 KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123, 321}));
802 KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123}));
803
804 KJ_EXPECT(ArrayPtr<const int>({123, 456}) == ArrayPtr<const short>({123, 456}));
805 KJ_EXPECT(!(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123, 456})));
806 KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123, 321}));
807 KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123}));
808
809 KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) ==
810 ArrayPtr<const char* const>({"foo", "bar"})));
811 KJ_EXPECT(!(ArrayPtr<const StringPtr>({"foo", "bar"}) !=
812 ArrayPtr<const char* const>({"foo", "bar"})));
813 KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) !=
814 ArrayPtr<const char* const>({"foo", "baz"})));
815 KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) !=
816 ArrayPtr<const char* const>({"foo"})));
817
818 // operator== should not use memcmp for double elements.
819 double d[1] = { nan() };
820 KJ_EXPECT(ArrayPtr<double>(d, 1) != ArrayPtr<double>(d, 1));
821 }
822
823 KJ_TEST("kj::range()") {
824 uint expected = 5;
825 for (uint i: range(5, 10)) {
826 KJ_EXPECT(i == expected++);
827 }
828 KJ_EXPECT(expected == 10);
829
830 expected = 0;
831 for (uint i: range(0, 8)) {
832 KJ_EXPECT(i == expected++);
833 }
834 KJ_EXPECT(expected == 8);
835 }
836
837 KJ_TEST("kj::defer()") {
838 {
839 // rvalue reference
840 bool executed = false;
841 {
842 auto deferred = kj::defer([&executed]() {
843 executed = true;
844 });
845 KJ_EXPECT(!executed);
846 }
847
848 KJ_EXPECT(executed);
849 }
850
851 {
852 // lvalue reference
853 bool executed = false;
854 auto executor = [&executed]() {
855 executed = true;
856 };
857
858 {
859 auto deferred = kj::defer(executor);
860 KJ_EXPECT(!executed);
861 }
862
863 KJ_EXPECT(executed);
864 }
865
866 {
867 // Cancellation via `cancel()`.
868 bool executed = false;
869 {
870 auto deferred = kj::defer([&executed]() {
871 executed = true;
872 });
873 KJ_EXPECT(!executed);
874
875 // Cancel and release the functor.
876 deferred.cancel();
877 KJ_EXPECT(!executed);
878 }
879
880 KJ_EXPECT(!executed);
881 }
882
883 {
884 // Execution via `run()`.
885 size_t runCount = 0;
886 {
887 auto deferred = kj::defer([&runCount](){
888 ++runCount;
889 });
890
891 // Run and release the functor.
892 deferred.run();
893 KJ_EXPECT(runCount == 1);
894 }
895
896 // `deferred` is already been run, so nothing is run when we destruct it.
897 KJ_EXPECT(runCount == 1);
898 }
899
900 }
901
902 KJ_TEST("kj::ArrayPtr startsWith / endsWith / findFirst / findLast") {
903 // Note: char-/byte- optimized versions are covered by string-test.c++.
904
905 int rawArray[] = {12, 34, 56, 34, 12};
906 ArrayPtr<int> arr(rawArray);
907
908 KJ_EXPECT(arr.startsWith({12, 34}));
909 KJ_EXPECT(arr.startsWith({12, 34, 56}));
910 KJ_EXPECT(!arr.startsWith({12, 34, 56, 78}));
911 KJ_EXPECT(arr.startsWith({12, 34, 56, 34, 12}));
912 KJ_EXPECT(!arr.startsWith({12, 34, 56, 34, 12, 12}));
913
914 KJ_EXPECT(arr.endsWith({34, 12}));
915 KJ_EXPECT(arr.endsWith({56, 34, 12}));
916 KJ_EXPECT(!arr.endsWith({78, 56, 34, 12}));
917 KJ_EXPECT(arr.endsWith({12, 34, 56, 34, 12}));
918 KJ_EXPECT(!arr.endsWith({12, 12, 34, 56, 34, 12}));
919
920 KJ_EXPECT(arr.findFirst(12).orDefault(100) == 0);
921 KJ_EXPECT(arr.findFirst(34).orDefault(100) == 1);
922 KJ_EXPECT(arr.findFirst(56).orDefault(100) == 2);
923 KJ_EXPECT(arr.findFirst(78).orDefault(100) == 100);
924
925 KJ_EXPECT(arr.findLast(12).orDefault(100) == 4);
926 KJ_EXPECT(arr.findLast(34).orDefault(100) == 3);
927 KJ_EXPECT(arr.findLast(56).orDefault(100) == 2);
928 KJ_EXPECT(arr.findLast(78).orDefault(100) == 100);
929 }
930
931 } // namespace
932 } // namespace kj