utils_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2019 The Android Open Source Project
  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 <gtest/gtest.h>
  17. #include <unordered_map>
  18. #include "utils.h"
  19. namespace {
  20. using ::android::bluetooth::audio::utils::ParseAudioParams;
  21. class UtilsTest : public testing::Test {
  22. protected:
  23. virtual void SetUp() {}
  24. virtual void TearDown() { map_.clear(); }
  25. std::unordered_map<std::string, std::string> map_;
  26. };
  27. TEST_F(UtilsTest, HashMapEmptyParams) {
  28. std::string params = "";
  29. map_ = ParseAudioParams(params);
  30. // map = {}
  31. EXPECT_TRUE(map_.empty());
  32. }
  33. TEST_F(UtilsTest, HashMapDelimitOnly) {
  34. std::string params = ";";
  35. map_ = ParseAudioParams(params);
  36. // map = {}
  37. EXPECT_TRUE(map_.empty());
  38. }
  39. TEST_F(UtilsTest, HashMapNotKeyValuePair) {
  40. std::string params = "key0";
  41. map_ = ParseAudioParams(params);
  42. // map = {[key0]=""}
  43. EXPECT_EQ(map_.size(), 1);
  44. EXPECT_NE(map_.find("key0"), map_.end());
  45. EXPECT_EQ(map_["key0"].length(), 0);
  46. }
  47. TEST_F(UtilsTest, HashMapEmptyValue) {
  48. std::string params = "key0=";
  49. map_ = ParseAudioParams(params);
  50. // map = {[key0]=""}
  51. EXPECT_EQ(map_.size(), 1);
  52. EXPECT_NE(map_.find("key0"), map_.end());
  53. EXPECT_EQ(map_["key0"].length(), 0);
  54. }
  55. TEST_F(UtilsTest, HashMapEmptyValueWithDelimit) {
  56. std::string params = "key0=;";
  57. map_ = ParseAudioParams(params);
  58. // map = {[key0]=""}
  59. EXPECT_EQ(map_.size(), 1);
  60. EXPECT_NE(map_.find("key0"), map_.end());
  61. EXPECT_EQ(map_["key0"].length(), 0);
  62. }
  63. TEST_F(UtilsTest, HashMapOneKeyValuePair) {
  64. std::string params = "key0=value0";
  65. map_ = ParseAudioParams(params);
  66. // map = {[key0]="value0"}
  67. EXPECT_EQ(map_.size(), 1);
  68. EXPECT_EQ(map_["key0"], "value0");
  69. }
  70. TEST_F(UtilsTest, HashMapOneKeyValuePairWithDelimit) {
  71. std::string params = "key0=value0;";
  72. map_ = ParseAudioParams(params);
  73. // map = {[key0]="value0"}
  74. EXPECT_EQ(map_.size(), 1);
  75. EXPECT_EQ(map_["key0"], "value0");
  76. }
  77. TEST_F(UtilsTest, HashMapTwoKeyValuePairs) {
  78. std::string params = "key0=value0;key1=value1";
  79. map_ = ParseAudioParams(params);
  80. // map = {[key0]="value0", [key1]="value1"}
  81. EXPECT_EQ(map_.size(), 2);
  82. EXPECT_EQ(map_["key0"], "value0");
  83. EXPECT_EQ(map_["key1"], "value1");
  84. }
  85. TEST_F(UtilsTest, HashMapEmptyKey) {
  86. std::string params = "=value";
  87. map_ = ParseAudioParams(params);
  88. // map = {}
  89. EXPECT_TRUE(map_.empty());
  90. }
  91. TEST_F(UtilsTest, HashMapEmptyKeyWithDelimit) {
  92. std::string params = "=value;";
  93. map_ = ParseAudioParams(params);
  94. // map = {}
  95. EXPECT_TRUE(map_.empty());
  96. }
  97. TEST_F(UtilsTest, HashMapEquivalentOnly) {
  98. std::string params = "=";
  99. map_ = ParseAudioParams(params);
  100. // map = {}
  101. EXPECT_TRUE(map_.empty());
  102. }
  103. TEST_F(UtilsTest, HashMapNoKeyValuePair) {
  104. std::string params = "=;";
  105. map_ = ParseAudioParams(params);
  106. // map = {}
  107. EXPECT_TRUE(map_.empty());
  108. }
  109. TEST_F(UtilsTest, HashMapTwoPairsWithFirstKeyEmpty) {
  110. std::string params = "=value0;key1=value1";
  111. map_ = ParseAudioParams(params);
  112. // map = {[key1]="value1"}
  113. EXPECT_EQ(map_.size(), 1);
  114. EXPECT_EQ(map_["key1"], "value1");
  115. }
  116. } // namespace