88 lines
1.7 KiB
C++
88 lines
1.7 KiB
C++
// This file is generated by mkfile.py
|
|
// Date: 2026-06-25
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <solution/8.h>
|
|
|
|
#include <climits>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
class MyAtoiTest : public ::testing::Test
|
|
{
|
|
protected:
|
|
void AssertMyAtoi(const std::string &input, int expected)
|
|
{
|
|
std::vector<char> buffer(input.begin(), input.end());
|
|
buffer.push_back('\0');
|
|
|
|
EXPECT_EQ(myAtoi(buffer.data()), expected);
|
|
}
|
|
};
|
|
|
|
TEST_F(MyAtoiTest, Example1)
|
|
{
|
|
AssertMyAtoi("42", 42);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, Example2)
|
|
{
|
|
AssertMyAtoi(" -42", -42);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, Example3)
|
|
{
|
|
AssertMyAtoi("4193 with words", 4193);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, ReturnsZeroWhenStringStartsWithNonDigit)
|
|
{
|
|
AssertMyAtoi("words and 987", 0);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, ReadsOptionalPlusSign)
|
|
{
|
|
AssertMyAtoi("+1", 1);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, RejectsMultipleOrSeparatedSigns)
|
|
{
|
|
AssertMyAtoi("+-12", 0);
|
|
AssertMyAtoi("-+12", 0);
|
|
AssertMyAtoi(" + 413", 0);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, StopsAtFirstNonDigitAfterReadingDigits)
|
|
{
|
|
AssertMyAtoi("3.14159", 3);
|
|
AssertMyAtoi("00000-42a1234", 0);
|
|
AssertMyAtoi(" +0 123", 0);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, HandlesEmptyAndSignOnlyStrings)
|
|
{
|
|
AssertMyAtoi("", 0);
|
|
AssertMyAtoi(" ", 0);
|
|
AssertMyAtoi("+", 0);
|
|
AssertMyAtoi("-", 0);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, ClampsPositiveOverflow)
|
|
{
|
|
AssertMyAtoi("2147483647", INT_MAX);
|
|
AssertMyAtoi("2147483648", INT_MAX);
|
|
AssertMyAtoi("91283472332", INT_MAX);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, ClampsNegativeOverflow)
|
|
{
|
|
AssertMyAtoi("-2147483648", INT_MIN);
|
|
AssertMyAtoi("-2147483649", INT_MIN);
|
|
AssertMyAtoi("-91283472332", INT_MIN);
|
|
}
|
|
|
|
TEST_F(MyAtoiTest, IgnoresLeadingZerosBeforeValidDigits)
|
|
{
|
|
AssertMyAtoi(" 0000000000012345678", 12345678);
|
|
}
|