Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: base/safe_format_unittest.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/safe_format.cc ('k') | base/scope_guard.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2010 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15 //
16 // SafeCStringFormat unit tests.
17
18 #include "omaha/base/safe_format.h"
19 #include "omaha/testing/unit_test.h"
20
21 namespace omaha {
22
23 TEST(SafeFormatTest, BrokenCStringFormatTruncates) {
24 // TODO(omaha): See http://b/1016121 for details. As of Sept 2010,
25 // CString::Format() is implemented using ::wvsprintf(), which has
26 // an internal 1024-byte buffer limit. A bug has been filed with
27 // Microsoft. If this test breaks, it means that we have gotten a
28 // new version of ATL/MFC with a fixed CString and can use that
29 // instead of SafeCStrFormat/AppendFormat.
30
31 TCHAR largestr[4000] = { 0 };
32
33 for (int i = 0; i < ARRAYSIZE(largestr); ++i) {
34 largestr[i] = _T('a') + static_cast<TCHAR>(i % 26);
35 }
36 largestr[ARRAYSIZE(largestr) - 1] = _T('\0');
37
38 CString test_string;
39 test_string.Format(_T("%s"), largestr);
40 EXPECT_EQ(1024, test_string.GetLength());
41 test_string.AppendFormat(_T("%s"), largestr);
42 EXPECT_EQ(2048, test_string.GetLength());
43 }
44
45 TEST(SafeFormatTest, SafeFormatDoesNotTruncate) {
46 TCHAR largestr[4000] = { 0 };
47
48 for (int i = 0; i < ARRAYSIZE(largestr); ++i) {
49 largestr[i] = _T('a') + static_cast<TCHAR>(i % 26);
50 }
51 largestr[ARRAYSIZE(largestr) - 1] = _T('\0');
52
53 CString test_string;
54 SafeCStringFormat(&test_string, _T("%s"), largestr);
55 EXPECT_EQ(ARRAYSIZE(largestr) - 1, test_string.GetLength());
56
57 SafeCStringAppendFormat(&test_string, _T("%s"), largestr);
58 EXPECT_EQ(2 * (ARRAYSIZE(largestr) - 1), test_string.GetLength());
59 }
60
61 TEST(SafeFormatTest, FormatBasicFieldTypes) {
62 CString test_string;
63
64 test_string.Empty();
65 SafeCStringFormat(&test_string, _T("%%"));
66 EXPECT_STREQ(_T("%"), test_string);
67
68 test_string.Empty();
69 SafeCStringFormat(&test_string, _T("%c"), _T('h'));
70 EXPECT_STREQ(_T("h"), test_string);
71
72 test_string.Empty();
73 SafeCStringFormat(&test_string, _T("%d"), -42);
74 EXPECT_STREQ(_T("-42"), test_string);
75
76 test_string.Empty();
77 SafeCStringFormat(&test_string, _T("%6u"), 1337);
78 EXPECT_STREQ(_T(" 1337"), test_string);
79
80 test_string.Empty();
81 SafeCStringFormat(&test_string, _T("%010X"), 3545084735U);
82 EXPECT_STREQ(_T("00D34DB33F"), test_string);
83
84 test_string.Empty();
85 SafeCStringFormat(&test_string, _T("%0.3f"), 123.456);
86 EXPECT_STREQ(_T("123.456"), test_string);
87
88 test_string.Empty();
89 SafeCStringFormat(&test_string, _T("\"%s\""), _T("ut_str"));
90 EXPECT_STREQ(_T("\"ut_str\""), test_string);
91
92 test_string.Empty();
93 SafeCStringFormat(&test_string, _T("\"%s\""), NULL);
94 EXPECT_STREQ(_T("\"(null)\""), test_string);
95 }
96
97 TEST(SafeFormatTest, AppendFormatBasicFieldTypes) {
98 const CString prefix = _T("ut_prefix");
99 CString test_string;
100
101 test_string = prefix;
102 SafeCStringAppendFormat(&test_string, _T("%%"));
103 EXPECT_STREQ(prefix + _T("%"), test_string);
104
105 test_string = prefix;
106 SafeCStringAppendFormat(&test_string, _T("%c"), _T('h'));
107 EXPECT_STREQ(prefix + _T("h"), test_string);
108
109 test_string = prefix;
110 SafeCStringAppendFormat(&test_string, _T("%d"), -42);
111 EXPECT_STREQ(prefix + _T("-42"), test_string);
112
113 test_string = prefix;
114 SafeCStringAppendFormat(&test_string, _T("%6u"), 1337);
115 EXPECT_STREQ(prefix + _T(" 1337"), test_string);
116
117 test_string = prefix;
118 SafeCStringAppendFormat(&test_string, _T("%010X"), 3545084735U);
119 EXPECT_STREQ(prefix + _T("00D34DB33F"), test_string);
120
121 test_string = prefix;
122 SafeCStringAppendFormat(&test_string, _T("%0.3f"), 123.456);
123 EXPECT_STREQ(prefix + _T("123.456"), test_string);
124
125 test_string = prefix;
126 SafeCStringAppendFormat(&test_string, _T("\"%s\""), _T("ut_str"));
127 EXPECT_STREQ(prefix + _T("\"ut_str\""), test_string);
128
129 test_string = prefix;
130 SafeCStringAppendFormat(&test_string, _T("\"%s\""), NULL);
131 EXPECT_STREQ(prefix + _T("\"(null)\""), test_string);
132 }
133
134 TEST(SafeFormatTest, FormatComplex) {
135 CString test_string = _T("prefix: ");
136 SafeCStringAppendFormat(&test_string, _T("Test: %cx%08X '%s' %4d %0.2f"),
137 _T('0'), 12648430, _T("utstr"), 42, 123.456);
138 EXPECT_STREQ(_T("prefix: Test: 0x00C0FFEE 'utstr' 42 123.46"), test_string);
139 }
140
141 } // namespace omaha
OLDNEW
« no previous file with comments | « base/safe_format.cc ('k') | base/scope_guard.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698