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

Side by Side Diff: base/string_util_unittest.cc

Issue 339059: Add compiler-specific "examine printf format" attributes to printfs. (Closed)
Patch Set: cleanups Created 11 years, 1 month 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <math.h> 5 #include <math.h>
6 #include <stdarg.h> 6 #include <stdarg.h>
7 7
8 #include <limits> 8 #include <limits>
9 #include <sstream> 9 #include <sstream>
10 10
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 EXPECT_STREQ(s1, s2); 820 EXPECT_STREQ(s1, s2);
821 EXPECT_EQ(d1, d2); 821 EXPECT_EQ(d1, d2);
822 822
823 va_end(dup); 823 va_end(dup);
824 } 824 }
825 825
826 TEST(StringUtilTest, VAList) { 826 TEST(StringUtilTest, VAList) {
827 VariableArgsFunc("%d %d %s %lf", 45, 92, "This is interesting", 9.21); 827 VariableArgsFunc("%d %d %s %lf", 45, 92, "This is interesting", 9.21);
828 } 828 }
829 829
830 TEST(StringUtilTest, StringPrintfEmptyFormat) { 830 TEST(StringUtilTest, StringPrintfEmpty) {
831 const char* empty = "";
832 EXPECT_EQ("", StringPrintf(empty));
833 EXPECT_EQ("", StringPrintf("%s", "")); 831 EXPECT_EQ("", StringPrintf("%s", ""));
834 } 832 }
835 833
836 TEST(StringUtilTest, StringPrintfMisc) { 834 TEST(StringUtilTest, StringPrintfMisc) {
837 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w')); 835 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w'));
838 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); 836 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w'));
839 } 837 }
840 838
841 TEST(StringUtilTest, StringAppendfStringEmptyParam) {
842 std::string value("Hello");
843 StringAppendF(&value, "");
844 EXPECT_EQ("Hello", value);
845
846 std::wstring valuew(L"Hello");
847 StringAppendF(&valuew, L"");
848 EXPECT_EQ(L"Hello", valuew);
849 }
850
851 TEST(StringUtilTest, StringAppendfEmptyString) { 839 TEST(StringUtilTest, StringAppendfEmptyString) {
852 std::string value("Hello"); 840 std::string value("Hello");
853 StringAppendF(&value, "%s", ""); 841 StringAppendF(&value, "%s", "");
854 EXPECT_EQ("Hello", value); 842 EXPECT_EQ("Hello", value);
855 843
856 std::wstring valuew(L"Hello"); 844 std::wstring valuew(L"Hello");
857 StringAppendF(&valuew, L"%ls", L""); 845 StringAppendF(&valuew, L"%ls", L"");
858 EXPECT_EQ(L"Hello", valuew); 846 EXPECT_EQ(L"Hello", valuew);
859 } 847 }
860 848
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 #if defined(OS_WIN) 907 #if defined(OS_WIN)
920 sprintf_s(ref, 320000, fmt, src, src, src, src, src, src, src); 908 sprintf_s(ref, 320000, fmt, src, src, src, src, src, src, src);
921 #elif defined(OS_POSIX) 909 #elif defined(OS_POSIX)
922 snprintf(ref, 320000, fmt, src, src, src, src, src, src, src); 910 snprintf(ref, 320000, fmt, src, src, src, src, src, src, src);
923 #endif 911 #endif
924 912
925 EXPECT_STREQ(ref, out.c_str()); 913 EXPECT_STREQ(ref, out.c_str());
926 delete[] ref; 914 delete[] ref;
927 } 915 }
928 916
917 // A helper for the StringAppendV test that follows.
918 // Just forwards its args to StringAppendV.
919 static void StringAppendVTestHelper(std::string* out,
Mark Mentovai 2009/11/18 20:55:27 Maybe mark this as PRINTF_FORMAT too.
920 const char* format,
921 ...) {
922 va_list ap;
923 va_start(ap, format);
924 StringAppendV(out, format, ap);
925 va_end(ap);
926 }
927
928 TEST(StringUtilTest, StringAppendV) {
929 std::string out;
930 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
931 EXPECT_EQ("1 foo bar", out);
932 }
933
929 // Test the boundary condition for the size of the string_util's 934 // Test the boundary condition for the size of the string_util's
930 // internal buffer. 935 // internal buffer.
931 TEST(StringUtilTest, GrowBoundary) { 936 TEST(StringUtilTest, GrowBoundary) {
932 const int string_util_buf_len = 1024; 937 const int string_util_buf_len = 1024;
933 // Our buffer should be one larger than the size of StringAppendVT's stack 938 // Our buffer should be one larger than the size of StringAppendVT's stack
934 // buffer. 939 // buffer.
935 const int buf_len = string_util_buf_len + 1; 940 const int buf_len = string_util_buf_len + 1;
936 char src[buf_len + 1]; // Need extra one for NULL-terminator. 941 char src[buf_len + 1]; // Need extra one for NULL-terminator.
937 for (int i = 0; i < buf_len; ++i) 942 for (int i = 0; i < buf_len; ++i)
938 src[i] = 'a'; 943 src[i] = 'a';
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 1347
1343 TEST(StringUtilTest, HexEncode) { 1348 TEST(StringUtilTest, HexEncode) {
1344 std::string hex(HexEncode(NULL, 0)); 1349 std::string hex(HexEncode(NULL, 0));
1345 EXPECT_EQ(hex.length(), 0U); 1350 EXPECT_EQ(hex.length(), 0U);
1346 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; 1351 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
1347 hex = HexEncode(bytes, sizeof(bytes)); 1352 hex = HexEncode(bytes, sizeof(bytes));
1348 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); 1353 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
1349 } 1354 }
1350 1355
1351 } // namaspace base 1356 } // namaspace base
OLDNEW
« no previous file with comments | « base/string_util.h ('k') | base/trace_event.cc » ('j') | chrome/app/chrome_dll_main.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698