Bladeren bron

Merge pull request #186 from MikeMirzayanov/dev-mikemirzayanov

Fixes in stringToLongLong/stringToUnsignedLongLong, more tests
Mike Mirzayanov 7 maanden geleden
bovenliggende
commit
37a1c5ca52

+ 32 - 46
testlib.h

@@ -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);
 
@@ -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());
 }
@@ -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);
 
@@ -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());
 }
@@ -3588,24 +3588,15 @@ 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
@@ -3613,24 +3604,21 @@ static inline long long stringToLongLong(InStream &in, const char *buffer) {
 
         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) {
@@ -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) {
@@ -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;

+ 1 - 9
tests/scripts/compile

@@ -33,20 +33,12 @@ 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
+  "$CPP" --version
   dir=$(dirname "$CPP")
   if [[ "$dir" == *"/bin" ]] || [[ "$MACHINE" == "Windows" ]]; then
     EXTRA_ARGS="${EXTRA_ARGS} -static"

+ 2 - 1
tests/scripts/test-ref

@@ -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
@@ -72,6 +72,7 @@ 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
 
   # Check exit code is the same

+ 53 - 21
tests/test-004_use-test.h/refs/r1/stderr

@@ -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 ()

+ 10 - 8
tests/test-004_use-test.h/refs/r1/stdout

@@ -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 - 0
tests/test-004_use-test.h/test.cpp

@@ -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();

+ 1 - 1
tests/test-004_use-test.h/tests/test-opts.cpp

@@ -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;

+ 23 - 0
tests/test-004_use-test.h/tests/test-stringToLongLong.cpp

@@ -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 - 0
tests/test-004_use-test.h/tests/test-stringToUnsignedLongLong.cpp

@@ -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");});
+}