id_type_unittest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2016 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 <limits>
  5. #include "base/types/id_type.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace base {
  8. namespace {
  9. class Foo;
  10. using FooId = IdType<Foo, int, 0>;
  11. } // namespace
  12. TEST(IdType, DefaultValueIsInvalid) {
  13. FooId foo_id;
  14. EXPECT_TRUE(foo_id.is_null());
  15. }
  16. TEST(IdType, NormalValueIsValid) {
  17. FooId foo_id = FooId::FromUnsafeValue(123);
  18. EXPECT_FALSE(foo_id.is_null());
  19. }
  20. TEST(IdType, Generator) {
  21. FooId::Generator foo_id_generator;
  22. for (int i = 1; i < 10; i++)
  23. EXPECT_EQ(foo_id_generator.GenerateNextId(), FooId::FromUnsafeValue(i));
  24. }
  25. TEST(IdType, GeneratorWithNonZeroInvalidValue) {
  26. using TestId = IdType<class TestIdTag, int, -1>;
  27. TestId::Generator test_id_generator;
  28. for (int i = 0; i < 10; i++)
  29. EXPECT_EQ(test_id_generator.GenerateNextId(), TestId::FromUnsafeValue(i));
  30. }
  31. TEST(IdType, GeneratorWithBigUnsignedInvalidValue) {
  32. using TestId =
  33. IdType<class TestIdTag, uint32_t, std::numeric_limits<uint32_t>::max()>;
  34. TestId::Generator test_id_generator;
  35. for (int i = 0; i < 10; i++) {
  36. TestId id = test_id_generator.GenerateNextId();
  37. EXPECT_FALSE(id.is_null());
  38. EXPECT_EQ(id, TestId::FromUnsafeValue(i));
  39. }
  40. }
  41. TEST(IdType, GeneratorWithDifferentStartingValue) {
  42. using TestId = IdType<class TestIdTag, int, -1, 1>;
  43. TestId::Generator test_id_generator;
  44. for (int i = 1; i < 10; i++)
  45. EXPECT_EQ(test_id_generator.GenerateNextId(), TestId::FromUnsafeValue(i));
  46. }
  47. TEST(IdType, EnsureConstexpr) {
  48. using TestId = IdType32<class TestTag>;
  49. // Test constructors.
  50. static constexpr TestId kZero;
  51. static constexpr auto kOne = TestId::FromUnsafeValue(1);
  52. // Test getting the underlying value.
  53. static_assert(kZero.value() == 0, "");
  54. static_assert(kOne.value() == 1, "");
  55. static_assert(kZero.GetUnsafeValue() == 0, "");
  56. static_assert(kOne.GetUnsafeValue() == 1, "");
  57. // Test is_null().
  58. static_assert(kZero.is_null(), "");
  59. static_assert(!kOne.is_null(), "");
  60. // Test operator bool.
  61. static_assert(!kZero, "");
  62. static_assert(kOne, "");
  63. }
  64. class IdTypeSpecificValueTest : public ::testing::TestWithParam<int> {
  65. protected:
  66. FooId test_id() { return FooId::FromUnsafeValue(GetParam()); }
  67. FooId other_id() {
  68. if (GetParam() != std::numeric_limits<int>::max())
  69. return FooId::FromUnsafeValue(GetParam() + 1);
  70. else
  71. return FooId::FromUnsafeValue(std::numeric_limits<int>::min());
  72. }
  73. };
  74. TEST_P(IdTypeSpecificValueTest, UnsafeValueRoundtrips) {
  75. int original_value = GetParam();
  76. FooId id = FooId::FromUnsafeValue(original_value);
  77. int final_value = id.GetUnsafeValue();
  78. EXPECT_EQ(original_value, final_value);
  79. }
  80. INSTANTIATE_TEST_SUITE_P(All,
  81. IdTypeSpecificValueTest,
  82. ::testing::Values(std::numeric_limits<int>::min(),
  83. -1,
  84. 0,
  85. 1,
  86. 123,
  87. std::numeric_limits<int>::max()));
  88. } // namespace base