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
28 #include "talk/base/gunit.h"
29 #include "talk/base/stringutils.h"
30 #include "talk/base/common.h"
31
32 namespace talk_base {
33
34 // Tests for string_match().
35
TEST(string_matchTest,Matches)36 TEST(string_matchTest, Matches) {
37 EXPECT_TRUE( string_match("A.B.C.D", "a.b.c.d"));
38 EXPECT_TRUE( string_match("www.TEST.GOOGLE.COM", "www.*.com"));
39 EXPECT_TRUE( string_match("127.0.0.1", "12*.0.*1"));
40 EXPECT_TRUE( string_match("127.1.0.21", "12*.0.*1"));
41 EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
42 EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
43 EXPECT_FALSE(string_match("127.1.1.21", "12*.0.*1"));
44 }
45
46 // It's not clear if we will ever use wchar_t strings on unix. In theory,
47 // all strings should be Utf8 all the time, except when interfacing with Win32
48 // APIs that require Utf16.
49
50 #ifdef WIN32
51
52 // Tests for ascii_string_compare().
53
54 // Tests NULL input.
TEST(ascii_string_compareTest,NullInput)55 TEST(ascii_string_compareTest, NullInput) {
56 // The following results in an access violation in
57 // ascii_string_compare. Is this a bug or by design? stringutils.h
58 // should document the expected behavior in this case.
59
60 // EXPECT_EQ(0, ascii_string_compare(NULL, NULL, 1, identity));
61 }
62
63 // Tests comparing two strings of different lengths.
TEST(ascii_string_compareTest,DifferentLengths)64 TEST(ascii_string_compareTest, DifferentLengths) {
65 EXPECT_EQ(-1, ascii_string_compare(L"Test", "Test1", 5, identity));
66 }
67
68 // Tests the case where the buffer size is smaller than the string
69 // lengths.
TEST(ascii_string_compareTest,SmallBuffer)70 TEST(ascii_string_compareTest, SmallBuffer) {
71 EXPECT_EQ(0, ascii_string_compare(L"Test", "Test1", 3, identity));
72 }
73
74 // Tests the case where the buffer is not full.
TEST(ascii_string_compareTest,LargeBuffer)75 TEST(ascii_string_compareTest, LargeBuffer) {
76 EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 10, identity));
77 }
78
79 // Tests comparing two eqaul strings.
TEST(ascii_string_compareTest,Equal)80 TEST(ascii_string_compareTest, Equal) {
81 EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 5, identity));
82 EXPECT_EQ(0, ascii_string_compare(L"TeSt", "tEsT", 5, tolowercase));
83 }
84
85 // Tests comparing a smller string to a larger one.
TEST(ascii_string_compareTest,LessThan)86 TEST(ascii_string_compareTest, LessThan) {
87 EXPECT_EQ(-1, ascii_string_compare(L"abc", "abd", 4, identity));
88 EXPECT_EQ(-1, ascii_string_compare(L"ABC", "abD", 5, tolowercase));
89 }
90
91 // Tests comparing a larger string to a smaller one.
TEST(ascii_string_compareTest,GreaterThan)92 TEST(ascii_string_compareTest, GreaterThan) {
93 EXPECT_EQ(1, ascii_string_compare(L"xyz", "xy", 5, identity));
94 EXPECT_EQ(1, ascii_string_compare(L"abc", "ABB", 5, tolowercase));
95 }
96 #endif // WIN32
97
TEST(string_trim_Test,Trimming)98 TEST(string_trim_Test, Trimming) {
99 EXPECT_EQ("temp", string_trim("\n\r\t temp \n\r\t"));
100 EXPECT_EQ("temp\n\r\t temp", string_trim(" temp\n\r\t temp "));
101 EXPECT_EQ("temp temp", string_trim("temp temp"));
102 EXPECT_EQ("", string_trim(" \r\n\t"));
103 EXPECT_EQ("", string_trim(""));
104 }
105
TEST(string_startsTest,StartsWith)106 TEST(string_startsTest, StartsWith) {
107 EXPECT_TRUE(starts_with("foobar", "foo"));
108 EXPECT_TRUE(starts_with("foobar", "foobar"));
109 EXPECT_TRUE(starts_with("foobar", ""));
110 EXPECT_TRUE(starts_with("", ""));
111 EXPECT_FALSE(starts_with("foobar", "bar"));
112 EXPECT_FALSE(starts_with("foobar", "foobarbaz"));
113 EXPECT_FALSE(starts_with("", "f"));
114 }
115
TEST(string_endsTest,EndsWith)116 TEST(string_endsTest, EndsWith) {
117 EXPECT_TRUE(ends_with("foobar", "bar"));
118 EXPECT_TRUE(ends_with("foobar", "foobar"));
119 EXPECT_TRUE(ends_with("foobar", ""));
120 EXPECT_TRUE(ends_with("", ""));
121 EXPECT_FALSE(ends_with("foobar", "foo"));
122 EXPECT_FALSE(ends_with("foobar", "foobarbaz"));
123 EXPECT_FALSE(ends_with("", "f"));
124 }
125
126 } // namespace talk_base
127