Skip to content

Commit

Permalink
Add posix compliance test for sscanf.
Browse files Browse the repository at this point in the history
b/307808954

Change-Id: I43619bfa46ba82b678eac499cf98565b455652bd
  • Loading branch information
yjzhang111 committed Aug 7, 2024
1 parent e8394fe commit 4b30064
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ target(gtest_target_type, "nplb") {
"posix_compliance/posix_string_compare_no_case_test.cc",
"posix_compliance/posix_string_format_test.cc",
"posix_compliance/posix_string_format_wide_test.cc",
"posix_compliance/posix_string_scan_test.cc",
"posix_compliance/posix_thread_attr_test.cc",
"posix_compliance/posix_thread_create_test.cc",
"posix_compliance/posix_thread_detach_test.cc",
Expand Down
95 changes: 95 additions & 0 deletions starboard/nplb/posix_compliance/posix_string_scan_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2016 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Here we are not trying to do anything fancy, just to really sanity check that
// this is hooked up to something.

#include "starboard/common/string.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

const char kIpGood[] = "192.168.1.2";
const char kIpBad1[] = "192.1x8.1.2";
const char kIpBad2[] = "192.1x8";
const char kIpBad3[] = "x.1.2.3";
const char kIpBad4[] = "192.168.1.2.";
const char kIpFormat[] = "%u.%u.%u.%u";

const char kFloatString[] = "3.14159";
const char kFloatPattern[] = "%f";
const char kDoublePattern[] = "%lf";
const float kExpectedFloat = 3.14159f;
const double kExpectedDouble = 3.14159;

TEST(PosixStringScanTest, SunnyDayIp1) {
unsigned int in[4] = {0};
EXPECT_EQ(4, sscanf(kIpGood, kIpFormat, in, in + 1, in + 2, in + 3));
EXPECT_EQ(192, in[0]);
EXPECT_EQ(168, in[1]);
EXPECT_EQ(1, in[2]);
EXPECT_EQ(2, in[3]);
}

TEST(PosixStringScanTest, SunnyDayIp2) {
unsigned int in[4] = {0};
EXPECT_EQ(4, sscanf(kIpBad4, kIpFormat, in, in + 1, in + 2, in + 3));
EXPECT_EQ(192, in[0]);
EXPECT_EQ(168, in[1]);
EXPECT_EQ(1, in[2]);
EXPECT_EQ(2, in[3]);
}

TEST(PosixStringScanTest, SunnyDayFloat) {
float f = 0.0f;
EXPECT_EQ(1, sscanf(kFloatString, kFloatPattern, &f));
EXPECT_EQ(kExpectedFloat, f);

double d = 0.0;
EXPECT_EQ(1, sscanf(kFloatString, kDoublePattern, &d));
EXPECT_EQ(kExpectedDouble, d);
}

TEST(PosixStringScanTest, RainyDayIp1) {
unsigned int in[4] = {0};
EXPECT_EQ(2, sscanf(kIpBad1, kIpFormat, in, in + 1, in + 2, in + 3));
EXPECT_EQ(192, in[0]);
EXPECT_EQ(1, in[1]);
EXPECT_EQ(0, in[2]);
EXPECT_EQ(0, in[3]);
}

TEST(PosixStringScanTest, RainyDayIp2) {
unsigned int in[4] = {0};
EXPECT_EQ(2, sscanf(kIpBad2, kIpFormat, in, in + 1, in + 2, in + 3));
EXPECT_EQ(192, in[0]);
EXPECT_EQ(1, in[1]);
EXPECT_EQ(0, in[2]);
EXPECT_EQ(0, in[3]);
}

TEST(PosixStringScanTest, RainyDayIp3) {
unsigned int in[4] = {0};
EXPECT_EQ(0, sscanf(kIpBad3, kIpFormat, in, in + 1, in + 2, in + 3));
EXPECT_EQ(0, in[0]);
EXPECT_EQ(0, in[1]);
EXPECT_EQ(0, in[2]);
EXPECT_EQ(0, in[3]);
}

} // namespace
} // namespace nplb
} // namespace starboard

0 comments on commit 4b30064

Please sign in to comment.