• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11 
12 #include "android/base/StringParse.h"
13 
14 #include "android/base/threads/FunctorThread.h"
15 #include "android/base/testing/Utils.h"
16 #include <gtest/gtest.h>
17 
18 namespace android {
19 namespace base {
20 
testScanf()21 static void testScanf() {
22     static const char comma[] = "1,3";
23     static const char dot[] = "1.3";
24     static const char format[] = "%f%n";
25     float val = 0;
26     int n = 0;
27 
28     // Now make sure we parse floating point strings as expected.
29     EXPECT_EQ(1, sscanf(dot, format, &val, &n));
30     EXPECT_FLOAT_EQ(1, val);
31     EXPECT_EQ(1, n);
32     EXPECT_EQ(1, sscanf(comma, format, &val, &n));
33     EXPECT_FLOAT_EQ(1.3, val);
34     EXPECT_EQ(3, n);
35 
36     // C-Locale parsing should be able to parse strings with C locale
37     EXPECT_EQ(1, SscanfWithCLocale(dot, format, &val, &n));
38     EXPECT_FLOAT_EQ(1.3, val);
39     EXPECT_EQ(3, n);
40 
41     // And the regular parsing still works as it used to.
42     EXPECT_EQ(1, sscanf(dot, format, &val, &n));
43     EXPECT_FLOAT_EQ(1, val);
44     EXPECT_EQ(1, n);
45     EXPECT_EQ(1, sscanf(comma, format, &val, &n));
46     EXPECT_FLOAT_EQ(1.3, val);
47     EXPECT_EQ(3, n);
48 }
49 
50 // These are flaky as they depend on the build env.
TEST(StringParse,DISABLED_SscanfWithCLocale)51 TEST(StringParse, DISABLED_SscanfWithCLocale) {
52     auto scopedCommaLocale = setScopedCommaLocale();
53     testScanf();
54 }
55 
TEST(StringParse,DISABLED_SscanfWithCLocaleThreads)56 TEST(StringParse, DISABLED_SscanfWithCLocaleThreads) {
57     auto scopedCommaLocale = setScopedCommaLocale();
58 
59     std::vector<std::unique_ptr<FunctorThread>> threads;
60     for (int i = 0; i < 20; ++i) {
61         threads.emplace_back(new FunctorThread(&testScanf));
62     }
63     for (auto& t : threads) {
64         ASSERT_TRUE(t->start());
65     }
66     for (auto& t : threads) {
67         ASSERT_TRUE(t->wait());
68     }
69 }
70 
71 }  // namespace base
72 }  // namespace android
73