Skip to content

Commit

Permalink
Merge branch 'master' into anywany-tests
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/ci.yml
#	testlib.h
#	tests/scripts/test-ref
  • Loading branch information
MikeMirzayanov committed Sep 28, 2023
2 parents 30d6a66 + 37a1c5c commit ffe5752
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 297 deletions.
427 changes: 226 additions & 201 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

78 changes: 32 additions & 46 deletions testlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3454,7 +3454,7 @@ static inline bool equals(unsigned long long integer, const char *s) {
}

static inline double stringToDouble(InStream &in, const char *buffer) {
double retval;
double result;

size_t length = strlen(buffer);

Expand Down Expand Up @@ -3488,14 +3488,14 @@ static inline double stringToDouble(InStream &in, const char *buffer) {

char *suffix = new char[length + 1];
std::memset(suffix, 0, length + 1);
int scanned = std::sscanf(buffer, "%lf%s", &retval, suffix);
int scanned = std::sscanf(buffer, "%lf%s", &result, suffix);
bool empty = strlen(suffix) == 0;
delete[] suffix;

if (scanned == 1 || (scanned == 2 && empty)) {
if (__testlib_isNaN(retval))
if (__testlib_isNaN(result))
in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
return retval;
return result;
} else
in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
}
Expand All @@ -3516,7 +3516,7 @@ static inline double stringToStrictDouble(InStream &in, const char *buffer,
in.quit(_fail,
"stringToStrictDouble: minAfterPointDigitCount should be less or equal to maxAfterPointDigitCount.");

double retval;
double result;

size_t length = strlen(buffer);

Expand Down Expand Up @@ -3565,16 +3565,16 @@ static inline double stringToStrictDouble(InStream &in, const char *buffer,

char *suffix = new char[length + 1];
std::memset(suffix, 0, length + 1);
int scanned = std::sscanf(buffer, "%lf%s", &retval, suffix);
int scanned = std::sscanf(buffer, "%lf%s", &result, suffix);
bool empty = strlen(suffix) == 0;
delete[] suffix;

if (scanned == 1 || (scanned == 2 && empty)) {
if (__testlib_isNaN(retval) || __testlib_isInfinite(retval))
if (__testlib_isNaN(result) || __testlib_isInfinite(result))
in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
if (buffer[0] == '-' && retval >= 0)
if (buffer[0] == '-' && result >= 0)
in.quit(_pe, ("Redundant minus in \"" + __testlib_part(buffer) + "\" found").c_str());
return retval;
return result;
} else
in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
}
Expand All @@ -3588,49 +3588,37 @@ static inline double stringToStrictDouble(InStream &in, const std::string& buffe
}

static inline long long stringToLongLong(InStream &in, const char *buffer) {
if (strcmp(buffer, "-9223372036854775808") == 0)
return LLONG_MIN;

bool minus = false;
size_t length = strlen(buffer);

if (length > 1 && buffer[0] == '-')
minus = true;

if (length > 20)
if (length == 0 || length > 20)
in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());

long long retval = 0LL;

bool has_minus = (length > 1 && buffer[0] == '-');
int zeroes = 0;
bool processingZeroes = true;

for (int i = (minus ? 1 : 0); i < int(length); i++) {
for (int i = (has_minus ? 1 : 0); i < int(length); i++) {
if (buffer[i] == '0' && processingZeroes)
zeroes++;
else
processingZeroes = false;

if (buffer[i] < '0' || buffer[i] > '9')
in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
retval = retval * 10 + (buffer[i] - '0');
}

if (retval < 0)
long long int result;
try {
result = std::stoll(buffer);
} catch (const std::exception&) {
in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());

if ((zeroes > 0 && (retval != 0 || minus)) || zeroes > 1)
} catch (...) {
in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
}

retval = (minus ? -retval : +retval);

if (length < 19)
return retval;
if ((zeroes > 0 && (result != 0 || has_minus)) || zeroes > 1)
in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());

if (equals(retval, buffer))
return retval;
else
in.quit(_pe, ("Expected int64, but \"" + __testlib_part(buffer) + "\" found").c_str());
return result;
}

static inline long long stringToLongLong(InStream &in, const std::string& buffer) {
Expand All @@ -3643,28 +3631,26 @@ static inline long long stringToLongLong(InStream &in, const std::string& buffer
static inline unsigned long long stringToUnsignedLongLong(InStream &in, const char *buffer) {
size_t length = strlen(buffer);

if (length > 20)
if (length == 0 || length > 20)
in.quit(_pe, ("Expected unsigned integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
if (length > 1 && buffer[0] == '0')
in.quit(_pe, ("Expected unsigned integer, but \"" + __testlib_part(buffer) + "\" found").c_str());

unsigned long long retval = 0LL;
for (int i = 0; i < int(length); i++) {
if (buffer[i] < '0' || buffer[i] > '9')
in.quit(_pe, ("Expected unsigned integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
retval = retval * 10 + (buffer[i] - '0');
}

if (length < 19)
return retval;

if (length == 20 && strcmp(buffer, "18446744073709551615") > 0)
in.quit(_pe, ("Expected unsigned int64, but \"" + __testlib_part(buffer) + "\" found").c_str());
unsigned long long result;
try {
result = std::stoull(buffer);
} catch (const std::exception&) {
in.quit(_pe, ("Expected unsigned integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
} catch (...) {
in.quit(_pe, ("Expected unsigned integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
}

if (equals(retval, buffer))
return retval;
else
in.quit(_pe, ("Expected unsigned int64, but \"" + __testlib_part(buffer) + "\" found").c_str());
return result;
}

static inline long long stringToUnsignedLongLong(InStream &in, const std::string& buffer) {
Expand Down Expand Up @@ -5443,7 +5429,7 @@ T optValueToIntegral(const std::string &s_, bool nonnegative) {
for (size_t i = pos; i < s.length(); i++) {
if (s[i] < '0' || s[i] > '9')
__testlib_fail("Opts: expected integer but '" + compress(s_) + "' found");
value = value * 10 + s[i] - '0';
value = T(value * 10 + s[i] - '0');
about = about * 10 + s[i] - '0';
}
value *= sign;
Expand Down
14 changes: 3 additions & 11 deletions tests/scripts/compile
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,17 @@ fi
rm -f "$exe_file"

EXTRA_ARGS=""
#if [[ "$CPP" == "clang++" && "$MACHINE" == "Windows" ]]; then
# msvc_version="2022"
# wk_version="10.0.19041.0"
# EXTRA_ARGS=" -I\"$TESTS_DIR/lib/msvc-$msvc_version-include\""
# for s in cppwinrt shared ucrt um winrt; do
# EXTRA_ARGS="$EXTRA_ARGS -I\"$TESTS_DIR/lib/windows-kit-$wk_version-include/$s\""
# done
#fi

if [[ "$CPP" == "cl.exe" ]]; then
echo "Compiling $src_file, running:" "$CPP" "$CPP_STANDARD" "-F268435456" "-EHsc" "-O2" -I"${CPP_INCLUDE_DIR}" -Fe"$exe_file" "$src_file"
"$CPP" "$CPP_STANDARD" "-F268435456" "-EHsc" "-O2" -I"${CPP_INCLUDE_DIR}" -Fe"$exe_file" "$src_file"
else
"$CPP" -v
echo "Compiling $src_file, running:" "$CPP" "$CPP_OPTS" "$CPP_STANDARD" -Wpedantic -Werror -I"${CPP_INCLUDE_DIR}""$EXTRA_ARGS" -o"$exe_file" -O2 "$src_file"
"$CPP" --version
dir=$(dirname "$CPP")
if [[ "$dir" == *"/bin" ]]; then
if [[ "$dir" == *"/bin" ]] || [[ "$MACHINE" == "Windows" ]]; then
EXTRA_ARGS="${EXTRA_ARGS} -static"
fi
echo "Compiling $src_file, running:" "$CPP" "$CPP_OPTS" "$CPP_STANDARD" -Wpedantic -Werror -I"${CPP_INCLUDE_DIR}""$EXTRA_ARGS" -o"$exe_file" -O2 "$src_file"
eval "$CPP" "$CPP_OPTS" "$CPP_STANDARD" -Wpedantic -Werror -I"${CPP_INCLUDE_DIR}""$EXTRA_ARGS" -o"$exe_file" -O2 "$src_file"
fi

Expand Down
11 changes: 2 additions & 9 deletions tests/scripts/test-ref
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ shift 1
# Check if we don't have invocation reference files
if [ ! -d "$refs" ]; then
if [[ "$TEST_REF_FORBID_GEN_REFS" == "true" ]]; then
echo "You forgot to run push ref files for invocation: "$*""
echo "You forgot to run push ref files for invocation: ""$*"""
echo "Run test locally, it will produce ref files and push it into the repo"
exit 1
fi
Expand All @@ -72,16 +72,9 @@ else
exit_code=0
# shellcheck disable=SC2048
$* 1>"$refs"/stdout.aux 2>"$refs"/stderr.aux || exit_code=$?
echo "Program exit code: $exit_code, stdout size: $(stat -c %s "$refs"/stdout.aux) bytes, stderr size: $(stat -c %s "$refs"/stderr.aux) bytes"
echo $exit_code >"$refs"/exit_code.aux

echo ---
cat "$refs"/stdout.aux
echo ---
cat "$refs"/stderr.aux
echo ---
cat "$refs"/exit_code.aux
echo ---

# Check exit code is the same
tester_lcmp_exit_code=0
"$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/exit_code.aux "$refs"/exit_code 2>tester-lcmp.out || tester_lcmp_exit_code=$?
Expand Down
74 changes: 53 additions & 21 deletions tests/test-004_use-test.h/refs/r1/stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
FAIL Opts: index '1' is out of range [0,1)
FAIL Opts: integer overflow: expected integer but '3e6' found
FAIL Opts: index '-1' is out of range [0,14)
FAIL Opts: opt by index '2': expected bool true/false or 0/1 but '-2147483648' found
FAIL Opts: expected integer but '-f1' found
FAIL Opts: index '14' is out of range [0,14)
FAIL Opts: expected non-negative integer but '-2147483648' found
FAIL Opts: unknown key 'test-count'
FAIL Opts: unknown key 'sum-n'
FAIL Opts: unused key 'min-val'
FAIL Opts: unused key 'max-val'
FAIL Opts: unused key 'min-length'
FAIL Opts: unused key 'bias-value'
FAIL Unexpected end of file - token expected (based on )
FAIL Unexpected end of file - token expected (based on )
FAIL Integer parameter [name=n] equals to 1, violates the range [0, 0] (based on )
FAIL Token parameter [name=n] equals to "abacaba", doesn't correspond to pattern "[abc]{6}" (based on )
FAIL Token parameter [name=n] equals to "abacab", doesn't correspond to pattern "a|test|abacaba|ok|abac" (based on )
FAIL Token parameter [name=n] equals to "abacabaa", doesn't correspond to pattern "a|test|abacaba|ok|abac" (based on )
FAIL Token parameter [name=n] equals to "abacaba!", doesn't correspond to pattern "a|test|abacaba|ok|abac" (based on )
FAIL Integer element a[4] equals to 4, violates the range [1, 3] (based on )
FAIL Opts: index '1' is out of range [0,1)
FAIL Opts: integer overflow: expected integer but '3e6' found
FAIL Opts: index '-1' is out of range [0,14)
FAIL Opts: opt by index '2': expected bool true/false or 0/1 but '-2147483648' found
FAIL Opts: expected integer but '-f1' found
FAIL Opts: index '14' is out of range [0,14)
FAIL Opts: expected non-negative integer but '-2147483648' found
FAIL Opts: unknown key 'test-count'
FAIL Opts: unknown key 'sum-n'
FAIL Opts: unused key 'min-val'
FAIL Opts: unused key 'max-val'
FAIL Opts: unused key 'min-length'
FAIL Opts: unused key 'bias-value'
FAIL Unexpected end of file - token expected (based on )
FAIL Unexpected end of file - token expected (based on )
FAIL Integer parameter [name=n] equals to 1, violates the range [0, 0] (based on )
FAIL Token parameter [name=n] equals to "abacaba", doesn't correspond to pattern "[abc]{6}" (based on )
FAIL Token parameter [name=n] equals to "abacab", doesn't correspond to pattern "a|test|abacaba|ok|abac" (based on )
FAIL Token parameter [name=n] equals to "abacabaa", doesn't correspond to pattern "a|test|abacaba|ok|abac" (based on )
FAIL Token parameter [name=n] equals to "abacaba!", doesn't correspond to pattern "a|test|abacaba|ok|abac" (based on )
FAIL Integer element a[4] equals to 4, violates the range [1, 3] (based on )
FAIL Expected integer, but "" found ()
FAIL Expected integer, but "-" found ()
FAIL Expected integer, but "+" found ()
FAIL Expected integer, but "00" found ()
FAIL Expected integer, but "0123" found ()
FAIL Expected integer, but "+123" found ()
FAIL Expected integer, but "09223372036854775807" found ()
FAIL Expected integer, but "9223372036854775808" found ()
FAIL Expected integer, but "-09223372036854775808" found ()
FAIL Expected integer, but "-9223372036854775809" found ()
FAIL Expected integer, but "1 " found ()
FAIL Expected integer, but " 1" found ()
FAIL Expected integer, but "1 2" found ()
FAIL Expected integer, but "-0" found ()
FAIL Expected integer, but "--0" found ()
FAIL Expected integer, but "123-" found ()
FAIL Expected unsigned integer, but "" found ()
FAIL Expected unsigned integer, but "-" found ()
FAIL Expected unsigned integer, but "-1" found ()
FAIL Expected unsigned integer, but "+" found ()
FAIL Expected unsigned integer, but "00" found ()
FAIL Expected unsigned integer, but "0123" found ()
FAIL Expected unsigned integer, but "+123" found ()
FAIL Expected unsigned integer, but "18446744073709551616" found ()
FAIL Expected unsigned integer, but "18446744073709551617" found ()
FAIL Expected unsigned integer, but "36893488147419103232" found ()
FAIL Expected unsigned integer, but "1 " found ()
FAIL Expected unsigned integer, but " 1" found ()
FAIL Expected unsigned integer, but "1 2" found ()
FAIL Expected unsigned integer, but "-0" found ()
FAIL Expected unsigned integer, but "--0" found ()
FAIL Expected unsigned integer, but "-00" found ()
18 changes: 10 additions & 8 deletions tests/test-004_use-test.h/refs/r1/stdout
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Running test 'join' OK
Running test 'split' OK
Running test 'tokenize' OK
Running test 'opts' OK
Running test 'instream' OK
Running test 'pattern' OK

SUCCESS 6 tests passed.
Running test 'join' OK
Running test 'split' OK
Running test 'tokenize' OK
Running test 'opts' OK
Running test 'instream' OK
Running test 'pattern' OK
Running test 'stringToLongLong' OK
Running test 'stringToUnsignedLongLong' OK

SUCCESS 8 tests passed.
2 changes: 2 additions & 0 deletions tests/test-004_use-test.h/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ using namespace std;
#include "tests/test-opts.cpp"
#include "tests/test-instream.cpp"
#include "tests/test-pattern.cpp"
#include "tests/test-stringToLongLong.cpp"
#include "tests/test-stringToUnsignedLongLong.cpp"

int main() {
disableFinalizeGuard();
Expand Down
2 changes: 1 addition & 1 deletion tests/test-004_use-test.h/tests/test-opts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST(opts) {
for (size_t i = 0; i < parts.size(); ++i) {
opts[i + 1] = parts[i].c_str();
}
prepareOpts(opts.size(), (char**)opts.data());
prepareOpts(int(opts.size()), (char**)opts.data());
}
{
GenArrayOpts res;
Expand Down
23 changes: 23 additions & 0 deletions tests/test-004_use-test.h/tests/test-stringToLongLong.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
TEST(stringToLongLong) {
ensure(stringToLongLong(inf, "1234567891") == 1234567891LL);
ensure(stringToLongLong(inf, "-47292722921111") == -47292722921111LL);
ensure(stringToLongLong(inf, "0") == 0LL);
ensure(stringToLongLong(inf, "9223372036854775807") == 9223372036854775807LL);
ensure(stringToLongLong(inf, "-9223372036854775808") == -9223372036854775807LL - 1LL);
ensure_exit(3, [](){stringToLongLong(inf, "");});
ensure_exit(3, [](){stringToLongLong(inf, "-");});
ensure_exit(3, [](){stringToLongLong(inf, "+");});
ensure_exit(3, [](){stringToLongLong(inf, "00");});
ensure_exit(3, [](){stringToLongLong(inf, "0123");});
ensure_exit(3, [](){stringToLongLong(inf, "+123");});
ensure_exit(3, [](){stringToLongLong(inf, "09223372036854775807");});
ensure_exit(3, [](){stringToLongLong(inf, "9223372036854775808");});
ensure_exit(3, [](){stringToLongLong(inf, "-09223372036854775808");});
ensure_exit(3, [](){stringToLongLong(inf, "-9223372036854775809");});
ensure_exit(3, [](){stringToLongLong(inf, "1 ");});
ensure_exit(3, [](){stringToLongLong(inf, " 1");});
ensure_exit(3, [](){stringToLongLong(inf, "1 2");});
ensure_exit(3, [](){stringToLongLong(inf, "-0");});
ensure_exit(3, [](){stringToLongLong(inf, "--0");});
ensure_exit(3, [](){stringToLongLong(inf, "123-");});
}
24 changes: 24 additions & 0 deletions tests/test-004_use-test.h/tests/test-stringToUnsignedLongLong.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TEST(stringToUnsignedLongLong) {
ensure(stringToUnsignedLongLong(inf, "123") == 123ULL);
ensure(stringToUnsignedLongLong(inf, "0") == 0ULL);
ensure(stringToUnsignedLongLong(inf, "7") == 7ULL);
ensure(stringToUnsignedLongLong(inf, "18446744073709551615") == 18446744073709551615ULL);
ensure(stringToUnsignedLongLong(inf, "9876543216540001000") == 9876543216540001000ULL);

ensure_exit(3, [](){stringToUnsignedLongLong(inf, "");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "-");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "-1");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "+");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "00");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "0123");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "+123");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "18446744073709551616");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "18446744073709551617");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "36893488147419103232");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "1 ");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, " 1");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "1 2");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "-0");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "--0");});
ensure_exit(3, [](){stringToUnsignedLongLong(inf, "-00");});
}

0 comments on commit ffe5752

Please sign in to comment.