1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/stringutils.h"
13 #include "webrtc/base/common.h"
14 
15 namespace rtc {
16 
17 // Tests for string_match().
18 
TEST(string_matchTest,Matches)19 TEST(string_matchTest, Matches) {
20   EXPECT_TRUE( string_match("A.B.C.D", "a.b.c.d"));
21   EXPECT_TRUE( string_match("www.TEST.GOOGLE.COM", "www.*.com"));
22   EXPECT_TRUE( string_match("127.0.0.1",  "12*.0.*1"));
23   EXPECT_TRUE( string_match("127.1.0.21", "12*.0.*1"));
24   EXPECT_FALSE(string_match("127.0.0.0",  "12*.0.*1"));
25   EXPECT_FALSE(string_match("127.0.0.0",  "12*.0.*1"));
26   EXPECT_FALSE(string_match("127.1.1.21", "12*.0.*1"));
27 }
28 
29 // It's not clear if we will ever use wchar_t strings on unix.  In theory,
30 // all strings should be Utf8 all the time, except when interfacing with Win32
31 // APIs that require Utf16.
32 
33 #if defined(WEBRTC_WIN)
34 
35 // Tests for ascii_string_compare().
36 
37 // Tests NULL input.
TEST(ascii_string_compareTest,NullInput)38 TEST(ascii_string_compareTest, NullInput) {
39   // The following results in an access violation in
40   // ascii_string_compare.  Is this a bug or by design?  stringutils.h
41   // should document the expected behavior in this case.
42 
43   // EXPECT_EQ(0, ascii_string_compare(NULL, NULL, 1, identity));
44 }
45 
46 // Tests comparing two strings of different lengths.
TEST(ascii_string_compareTest,DifferentLengths)47 TEST(ascii_string_compareTest, DifferentLengths) {
48   EXPECT_EQ(-1, ascii_string_compare(L"Test", "Test1", 5, identity));
49 }
50 
51 // Tests the case where the buffer size is smaller than the string
52 // lengths.
TEST(ascii_string_compareTest,SmallBuffer)53 TEST(ascii_string_compareTest, SmallBuffer) {
54   EXPECT_EQ(0, ascii_string_compare(L"Test", "Test1", 3, identity));
55 }
56 
57 // Tests the case where the buffer is not full.
TEST(ascii_string_compareTest,LargeBuffer)58 TEST(ascii_string_compareTest, LargeBuffer) {
59   EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 10, identity));
60 }
61 
62 // Tests comparing two eqaul strings.
TEST(ascii_string_compareTest,Equal)63 TEST(ascii_string_compareTest, Equal) {
64   EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 5, identity));
65   EXPECT_EQ(0, ascii_string_compare(L"TeSt", "tEsT", 5, tolowercase));
66 }
67 
68 // Tests comparing a smller string to a larger one.
TEST(ascii_string_compareTest,LessThan)69 TEST(ascii_string_compareTest, LessThan) {
70   EXPECT_EQ(-1, ascii_string_compare(L"abc", "abd", 4, identity));
71   EXPECT_EQ(-1, ascii_string_compare(L"ABC", "abD", 5, tolowercase));
72 }
73 
74 // Tests comparing a larger string to a smaller one.
TEST(ascii_string_compareTest,GreaterThan)75 TEST(ascii_string_compareTest, GreaterThan) {
76   EXPECT_EQ(1, ascii_string_compare(L"xyz", "xy", 5, identity));
77   EXPECT_EQ(1, ascii_string_compare(L"abc", "ABB", 5, tolowercase));
78 }
79 #endif  // WEBRTC_WIN
80 
TEST(string_trim_Test,Trimming)81 TEST(string_trim_Test, Trimming) {
82   EXPECT_EQ("temp", string_trim("\n\r\t temp \n\r\t"));
83   EXPECT_EQ("temp\n\r\t temp", string_trim(" temp\n\r\t temp "));
84   EXPECT_EQ("temp temp", string_trim("temp temp"));
85   EXPECT_EQ("", string_trim(" \r\n\t"));
86   EXPECT_EQ("", string_trim(""));
87 }
88 
TEST(string_startsTest,StartsWith)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 TEST(string_endsTest, EndsWith) {
100   EXPECT_TRUE(ends_with("foobar", "bar"));
101   EXPECT_TRUE(ends_with("foobar", "foobar"));
102   EXPECT_TRUE(ends_with("foobar", ""));
103   EXPECT_TRUE(ends_with("", ""));
104   EXPECT_FALSE(ends_with("foobar", "foo"));
105   EXPECT_FALSE(ends_with("foobar", "foobarbaz"));
106   EXPECT_FALSE(ends_with("", "f"));
107 }
108 
109 } // namespace rtc
110