You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

counter_test.cc 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "utils/counter.h"
  17. #include "common/common_test.h"
  18. namespace mindspore {
  19. class TestCounter : public UT::Common {
  20. public:
  21. TestCounter() {
  22. std::string s1 = "abcdeedfrgbhrtfsfd";
  23. std::string s2 = "shceufhvogawrycawr";
  24. for (auto c : s1) {
  25. std::string key(1, c);
  26. counter_a[key] += 1;
  27. }
  28. for (auto c : s2) {
  29. std::string key(1, c);
  30. counter_b[key] += 1;
  31. }
  32. }
  33. public:
  34. Counter<std::string> counter_a;
  35. Counter<std::string> counter_b;
  36. };
  37. TEST_F(TestCounter, test_constructor) {
  38. assert(counter_a.size() == 11);
  39. assert(counter_b.size() == 13);
  40. }
  41. TEST_F(TestCounter, test_subtitle) {
  42. std::string s = "d";
  43. assert(counter_a[s] == 3);
  44. s = "f";
  45. assert(counter_a[s] == 3);
  46. s = "h";
  47. assert(counter_b[s] = 2);
  48. s = "c";
  49. assert(counter_b[s] = 2);
  50. }
  51. TEST_F(TestCounter, test_contains) {
  52. std::string s = "d";
  53. assert(counter_a.contains(s) == true);
  54. s = "z";
  55. assert(counter_a.contains(s) == false);
  56. s = "q";
  57. assert(counter_b.contains(s) == false);
  58. }
  59. TEST_F(TestCounter, test_add) {
  60. auto counter_add = counter_a + counter_b;
  61. assert(counter_add.size() == 16);
  62. std::string s = "f";
  63. assert(counter_add[s] == 4);
  64. s = "r";
  65. assert(counter_add[s] == 4);
  66. s = "y";
  67. assert(counter_add[s] == 1);
  68. }
  69. TEST_F(TestCounter, test_minus) {
  70. auto counter_minus = counter_a - counter_b;
  71. assert(counter_minus.size() == 5);
  72. std::string s = "d";
  73. assert(counter_minus[s] == 3);
  74. s = "t";
  75. assert(counter_minus[s] == 1);
  76. s = "a";
  77. assert(counter_minus.contains(s) == false);
  78. }
  79. struct MyStruct {
  80. int a = 0;
  81. int b = 0;
  82. };
  83. struct MyHash {
  84. std::size_t operator()(const MyStruct &e) const noexcept { //
  85. return (static_cast<std::size_t>(e.a) << 16) + e.b;
  86. }
  87. };
  88. struct MyEqual {
  89. bool operator()(const MyStruct &lhs, const MyStruct &rhs) const noexcept { //
  90. return lhs.a == rhs.a && lhs.b == rhs.b;
  91. }
  92. };
  93. TEST_F(TestCounter, test_struct) {
  94. using MyCounter = Counter<MyStruct, MyHash, MyEqual>;
  95. MyCounter counter;
  96. counter.add(MyStruct{100, 1});
  97. counter.add(MyStruct{100, 2});
  98. counter.add(MyStruct{100, 2});
  99. counter.add(MyStruct{100, 3});
  100. counter.add(MyStruct{100, 3});
  101. counter.add(MyStruct{100, 3});
  102. ASSERT_EQ(1, (counter[MyStruct{100, 1}]));
  103. ASSERT_EQ(2, (counter[MyStruct{100, 2}]));
  104. ASSERT_EQ(3, (counter[MyStruct{100, 3}]));
  105. MyCounter counter2;
  106. counter2.add(MyStruct{100, 2});
  107. counter2.add(MyStruct{100, 3});
  108. counter2.add(MyStruct{100, 3});
  109. counter2.add(MyStruct{100, 3});
  110. counter2.add(MyStruct{100, 4});
  111. auto result = counter.subtract(counter2);
  112. ASSERT_EQ(2, result.size());
  113. ASSERT_TRUE((MyEqual{}(MyStruct{100, 1}, result[0])));
  114. ASSERT_TRUE((MyEqual{}(MyStruct{100, 2}, result[1])));
  115. counter2 = counter;
  116. ASSERT_EQ(3, counter2.size());
  117. ASSERT_EQ(1, (counter2[MyStruct{100, 1}]));
  118. ASSERT_EQ(2, (counter2[MyStruct{100, 2}]));
  119. ASSERT_EQ(3, (counter2[MyStruct{100, 3}]));
  120. }
  121. } // namespace mindspore