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 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. } // namespace mindspore