8
Gitea CTest Workflow / test (push) Waiting to run

This commit is contained in:
2026-06-25 14:21:11 +08:00
parent 7d45cb5978
commit 5f803ed09f
3 changed files with 159 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2026-06-25
#ifndef INC_8_H
#define INC_8_H
#ifdef __cplusplus
extern "C"
{
#endif
int myAtoi(char *s);
#ifdef __cplusplus
}
#endif
#endif // INC_8_H
+58
View File
@@ -0,0 +1,58 @@
// This file is generated by mkfile.py
// Date: 2026-06-25
#include <solution/8.h>
#include <limits.h>
#include <stdbool.h>
int myAtoi(char *s)
{
long ret = 0;
int length = 0;
bool isPos = true;
bool hasSign = false;
bool hasDigit = false;
bool hasSpace = true;
for (; *s != '\0' && length < 11; s++)
{
// step 1 skip space
if (hasSpace)
{
if (*s == ' ')
continue;
hasSpace = false;
}
// step 2 check is sign while has no number
if (!hasDigit && !hasSign && (*s == '-' || *s == '+'))
{
hasSign = true;
isPos = *s == '+';
continue;
}
// step 3 read num
if (*s - '0' >= 0 && *s - '9' <= 0)
{
hasDigit = true;
// check if front zero
if (length == 0 && *s == '0')
continue;
// not front zero
if (length > 0)
ret *= 10;
ret += *s - '0';
length++;
continue;
}
// step 4 check char
break;
}
if (isPos)
ret = ret > INT_MAX ? INT_MAX : ret;
else
ret = ret * -1 < INT_MIN ? INT_MIN : ret * -1;
return (int)ret;
}
+87
View File
@@ -0,0 +1,87 @@
// 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);
}