maybe_test.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "maybe.h"
  5. #include <string>
  6. #include <vector>
  7. #include "test_platform.h"
  8. namespace crdtp {
  9. // =============================================================================
  10. // detail::PtrMaybe, detail::ValueMaybe, templates for optional
  11. // pointers / values which are used in ../lib/Forward_h.template.
  12. // =============================================================================
  13. TEST(PtrMaybeTest, SmokeTest) {
  14. detail::PtrMaybe<std::vector<uint32_t>> example;
  15. EXPECT_FALSE(example.isJust());
  16. EXPECT_TRUE(nullptr == example.fromMaybe(nullptr));
  17. std::unique_ptr<std::vector<uint32_t>> v(new std::vector<uint32_t>);
  18. v->push_back(42);
  19. v->push_back(21);
  20. example = std::move(v);
  21. EXPECT_TRUE(example.isJust());
  22. EXPECT_THAT(*example.fromJust(), testing::ElementsAre(42, 21));
  23. std::unique_ptr<std::vector<uint32_t>> out = example.takeJust();
  24. EXPECT_FALSE(example.isJust());
  25. EXPECT_THAT(*out, testing::ElementsAre(42, 21));
  26. }
  27. TEST(PtrValueTest, SmokeTest) {
  28. detail::ValueMaybe<int32_t> example;
  29. EXPECT_FALSE(example.isJust());
  30. EXPECT_EQ(-1, example.fromMaybe(-1));
  31. example = 42;
  32. EXPECT_TRUE(example.isJust());
  33. EXPECT_EQ(42, example.fromJust());
  34. int32_t out = example.takeJust();
  35. EXPECT_EQ(out, 42);
  36. }
  37. } // namespace crdtp