Flutter Engine
The Flutter Engine
string_utils_unittest.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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.
4
5#include "string_utils.h"
6
7#include <cerrno>
8#include <cstddef>
9#include <string>
10
11#include "base/logging.h"
12#include "gtest/gtest.h"
13
14namespace base {
15
16TEST(StringUtilsTest, StringPrintfEmpty) {
17 EXPECT_EQ("", base::StringPrintf("%s", ""));
18}
19
20TEST(StringUtilsTest, StringPrintfMisc) {
21 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w'));
22}
23// Test that StringPrintf and StringAppendV do not change errno.
24TEST(StringUtilsTest, StringPrintfErrno) {
25 errno = 1;
26 EXPECT_EQ("", StringPrintf("%s", ""));
27 EXPECT_EQ(1, errno);
28}
29
30TEST(StringUtilsTest, canASCIIToUTF16) {
31 std::string ascii = "abcdefg";
32 EXPECT_EQ(ASCIIToUTF16(ascii).compare(u"abcdefg"), 0);
33}
34
35TEST(StringUtilsTest, canUTF8ToUTF16) {
36 std::string utf8 = "äåè";
37 EXPECT_EQ(UTF8ToUTF16(utf8).compare(u"äåè"), 0);
38}
39
40TEST(StringUtilsTest, canUTF16ToUTF8) {
41 std::u16string utf16 = u"äåè";
42 EXPECT_EQ(UTF16ToUTF8(utf16).compare("äåè"), 0);
43}
44
45TEST(StringUtilsTest, canNumberToString16) {
46 EXPECT_EQ(NumberToString16(1.123f), std::u16string(u"1.123"));
47}
48
49TEST(StringUtilsTest, numberToStringSimplifiesOutput) {
50 EXPECT_STREQ(NumberToString(0.0).c_str(), "0");
51 EXPECT_STREQ(NumberToString(0.0f).c_str(), "0");
52 EXPECT_STREQ(NumberToString(1.123).c_str(), "1.123");
53 EXPECT_STREQ(NumberToString(1.123f).c_str(), "1.123");
54 EXPECT_STREQ(NumberToString(-1.123).c_str(), "-1.123");
55 EXPECT_STREQ(NumberToString(-1.123f).c_str(), "-1.123");
56 EXPECT_STREQ(NumberToString(1.00001).c_str(), "1.00001");
57 EXPECT_STREQ(NumberToString(1.00001f).c_str(), "1.00001");
58 EXPECT_STREQ(NumberToString(1000.000001).c_str(), "1000.000001");
59 EXPECT_STREQ(NumberToString(10.00001f).c_str(), "10.00001");
60 EXPECT_STREQ(NumberToString(1.0 + 1e-8).c_str(), "1.00000001");
61 EXPECT_STREQ(NumberToString(1.0f + 1e-8f).c_str(), "1");
62 EXPECT_STREQ(NumberToString(1e-6).c_str(), "0.000001");
63 EXPECT_STREQ(NumberToString(1e-6f).c_str(), "0.000001");
64 EXPECT_STREQ(NumberToString(1e-8).c_str(), "1e-8");
65 EXPECT_STREQ(NumberToString(1e-8f).c_str(), "1e-8");
66 EXPECT_STREQ(NumberToString(100.0).c_str(), "100");
67 EXPECT_STREQ(NumberToString(100.0f).c_str(), "100");
68 EXPECT_STREQ(NumberToString(-1.0 - 1e-7).c_str(), "-1.0000001");
69 EXPECT_STREQ(NumberToString(-1.0f - 1e-7f).c_str(), "-1.0000001");
70 EXPECT_STREQ(NumberToString(0.00000012345678).c_str(), "1.2345678e-7");
71 // Difference in output is due to differences in double and float precision.
72 EXPECT_STREQ(NumberToString(0.00000012345678f).c_str(), "1.2345679e-7");
73 EXPECT_STREQ(NumberToString(-0.00000012345678).c_str(), "-1.2345678e-7");
74 // Difference in output is due to differences in double and float precision.
75 EXPECT_STREQ(NumberToString(-0.00000012345678f).c_str(), "-1.2345679e-7");
76 EXPECT_STREQ(NumberToString(static_cast<unsigned int>(11)).c_str(), "11");
77 EXPECT_STREQ(NumberToString(static_cast<int32_t>(-23)).c_str(), "-23");
78}
79
80} // namespace base
std::string StringPrintf(const std::string &format, Args... args)
Definition: string_utils.h:18
TEST(StringUtilsTest, StringPrintfEmpty)
std::string UTF16ToUTF8(std::u16string src)
Definition: string_utils.cc:71
std::u16string UTF8ToUTF16(std::string src)
Definition: string_utils.cc:67
std::u16string ASCIIToUTF16(std::string src)
Definition: string_utils.cc:63
std::string NumberToString(int32_t number)
Definition: string_utils.cc:91
std::u16string NumberToString16(float number)
Definition: string_utils.cc:75
int compare(const void *untyped_lhs, const void *untyped_rhs)
Definition: skdiff.h:161