1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/basictypes.h"
10 #include "chrome/installer/mini_installer/mini_string.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using mini_installer::StackString
;
16 class MiniInstallerStringTest
: public testing::Test
{
18 virtual void SetUp() {
20 virtual void TearDown() {
25 // Tests the strcat/strcpy/length support of the StackString class.
26 TEST_F(MiniInstallerStringTest
, StackStringOverflow
) {
27 static const wchar_t kTestString
[] = L
"1234567890";
29 StackString
<MAX_PATH
> str
;
30 EXPECT_EQ(MAX_PATH
, str
.capacity());
32 std::wstring compare_str
;
34 EXPECT_EQ(str
.length(), compare_str
.length());
35 EXPECT_EQ(0, compare_str
.compare(str
.get()));
37 size_t max_chars
= str
.capacity() - 1;
39 while ((str
.length() + (arraysize(kTestString
) - 1)) <= max_chars
) {
40 EXPECT_TRUE(str
.append(kTestString
));
41 compare_str
.append(kTestString
);
42 EXPECT_EQ(str
.length(), compare_str
.length());
43 EXPECT_EQ(0, compare_str
.compare(str
.get()));
46 EXPECT_GT(static_cast<size_t>(MAX_PATH
), str
.length());
48 // Now we've exhausted the space we allocated for the string,
49 // so append should fail.
50 EXPECT_FALSE(str
.append(kTestString
));
52 // ...and remain unchanged.
53 EXPECT_EQ(0, compare_str
.compare(str
.get()));
54 EXPECT_EQ(str
.length(), compare_str
.length());
59 EXPECT_EQ(0, compare_str
.compare(str
.get()));
60 EXPECT_EQ(str
.length(), compare_str
.length());
63 // Tests the case insensitive find support of the StackString class.
64 TEST_F(MiniInstallerStringTest
, StackStringFind
) {
65 static const wchar_t kTestStringSource
[] = L
"1234ABcD567890";
66 static const wchar_t kTestStringFind
[] = L
"abcd";
67 static const wchar_t kTestStringNotFound
[] = L
"80";
69 StackString
<MAX_PATH
> str
;
70 EXPECT_TRUE(str
.assign(kTestStringSource
));
71 EXPECT_EQ(str
.get(), str
.findi(kTestStringSource
));
72 EXPECT_EQ(static_cast<const wchar_t*>(NULL
), str
.findi(kTestStringNotFound
));
73 const wchar_t* found
= str
.findi(kTestStringFind
);
74 EXPECT_NE(static_cast<const wchar_t*>(NULL
), found
);
75 std::wstring
check(found
, arraysize(kTestStringFind
) - 1);
76 EXPECT_EQ(0, lstrcmpi(check
.c_str(), kTestStringFind
));