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

126 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/gunit.h"
  28. #include "talk/base/stringutils.h"
  29. #include "talk/base/common.h"
  30. namespace talk_base {
  31. // Tests for string_match().
  32. TEST(string_matchTest, Matches) {
  33. EXPECT_TRUE( string_match("A.B.C.D", "a.b.c.d"));
  34. EXPECT_TRUE( string_match("www.TEST.GOOGLE.COM", "www.*.com"));
  35. EXPECT_TRUE( string_match("127.0.0.1", "12*.0.*1"));
  36. EXPECT_TRUE( string_match("127.1.0.21", "12*.0.*1"));
  37. EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
  38. EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
  39. EXPECT_FALSE(string_match("127.1.1.21", "12*.0.*1"));
  40. }
  41. // It's not clear if we will ever use wchar_t strings on unix. In theory,
  42. // all strings should be Utf8 all the time, except when interfacing with Win32
  43. // APIs that require Utf16.
  44. #ifdef WIN32
  45. // Tests for ascii_string_compare().
  46. // Tests NULL input.
  47. TEST(ascii_string_compareTest, NullInput) {
  48. // The following results in an access violation in
  49. // ascii_string_compare. Is this a bug or by design? stringutils.h
  50. // should document the expected behavior in this case.
  51. // EXPECT_EQ(0, ascii_string_compare(NULL, NULL, 1, identity));
  52. }
  53. // Tests comparing two strings of different lengths.
  54. TEST(ascii_string_compareTest, DifferentLengths) {
  55. EXPECT_EQ(-1, ascii_string_compare(L"Test", "Test1", 5, identity));
  56. }
  57. // Tests the case where the buffer size is smaller than the string
  58. // lengths.
  59. TEST(ascii_string_compareTest, SmallBuffer) {
  60. EXPECT_EQ(0, ascii_string_compare(L"Test", "Test1", 3, identity));
  61. }
  62. // Tests the case where the buffer is not full.
  63. TEST(ascii_string_compareTest, LargeBuffer) {
  64. EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 10, identity));
  65. }
  66. // Tests comparing two eqaul strings.
  67. TEST(ascii_string_compareTest, Equal) {
  68. EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 5, identity));
  69. EXPECT_EQ(0, ascii_string_compare(L"TeSt", "tEsT", 5, tolowercase));
  70. }
  71. // Tests comparing a smller string to a larger one.
  72. TEST(ascii_string_compareTest, LessThan) {
  73. EXPECT_EQ(-1, ascii_string_compare(L"abc", "abd", 4, identity));
  74. EXPECT_EQ(-1, ascii_string_compare(L"ABC", "abD", 5, tolowercase));
  75. }
  76. // Tests comparing a larger string to a smaller one.
  77. TEST(ascii_string_compareTest, GreaterThan) {
  78. EXPECT_EQ(1, ascii_string_compare(L"xyz", "xy", 5, identity));
  79. EXPECT_EQ(1, ascii_string_compare(L"abc", "ABB", 5, tolowercase));
  80. }
  81. #endif // WIN32
  82. TEST(string_trim_Test, Trimming) {
  83. EXPECT_EQ("temp", string_trim("\n\r\t temp \n\r\t"));
  84. EXPECT_EQ("temp\n\r\t temp", string_trim(" temp\n\r\t temp "));
  85. EXPECT_EQ("temp temp", string_trim("temp temp"));
  86. EXPECT_EQ("", string_trim(" \r\n\t"));
  87. EXPECT_EQ("", string_trim(""));
  88. }
  89. TEST(string_startsTest, StartsWith) {
  90. EXPECT_TRUE(starts_with("foobar", "foo"));
  91. EXPECT_TRUE(starts_with("foobar", "foobar"));
  92. EXPECT_TRUE(starts_with("foobar", ""));
  93. EXPECT_TRUE(starts_with("", ""));
  94. EXPECT_FALSE(starts_with("foobar", "bar"));
  95. EXPECT_FALSE(starts_with("foobar", "foobarbaz"));
  96. EXPECT_FALSE(starts_with("", "f"));
  97. }
  98. TEST(string_endsTest, EndsWith) {
  99. EXPECT_TRUE(ends_with("foobar", "bar"));
  100. EXPECT_TRUE(ends_with("foobar", "foobar"));
  101. EXPECT_TRUE(ends_with("foobar", ""));
  102. EXPECT_TRUE(ends_with("", ""));
  103. EXPECT_FALSE(ends_with("foobar", "foo"));
  104. EXPECT_FALSE(ends_with("foobar", "foobarbaz"));
  105. EXPECT_FALSE(ends_with("", "f"));
  106. }
  107. } // namespace talk_base