Pārlūkot izejas kodu

better stringToLongLong/stringToUnsignedLongLong

mikemirzayanov 7 mēneši atpakaļ
vecāks
revīzija
4c57080f68

+ 20 - 33
testlib.h

@@ -3588,23 +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 == 0 || length > 20)
         in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
 
-    if (length > 1 && buffer[0] == '-')
-        minus = true;
-
-    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
@@ -3612,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 retval;
+    try {
+        retval = std::stoll(buffer);
+    } catch (const std::out_of_range& ignored) {
         in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
-
-    if ((zeroes > 0 && (retval != 0 || minus)) || zeroes > 1)
+    } catch (const std::invalid_argument& ignored) {
         in.quit(_pe, ("Expected integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
+    }
 
-    retval = (minus ? -retval : +retval);
-
-    if (length < 19)
-        return retval;
+    if ((zeroes > 0 && (retval != 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 retval;
 }
 
 static inline long long stringToLongLong(InStream &in, const std::string& buffer) {
@@ -3647,23 +3636,21 @@ static inline unsigned long long stringToUnsignedLongLong(InStream &in, const ch
     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 retval;
+    try {
+        retval = std::stoull(buffer);
+    } catch (const std::out_of_range& ignored) {
+        in.quit(_pe, ("Expected unsigned integer, but \"" + __testlib_part(buffer) + "\" found").c_str());
+    } catch (const std::invalid_argument& ignored) {
+        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 retval;
 }
 
 static inline long long stringToUnsignedLongLong(InStream &in, const std::string& buffer) {

+ 1 - 1
tests/scripts/compile

@@ -46,7 +46,7 @@ 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"

+ 1 - 0
tests/scripts/test-ref

@@ -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 - 33
tests/test-004_use-test.h/refs/r1/stderr

@@ -1,33 +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 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 "9223372036854775808" 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 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 - 9
tests/test-004_use-test.h/refs/r1/stdout

@@ -1,9 +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
-Running test 'stringToLongLong'                                                                     OK
-
-SUCCESS 7 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.

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

@@ -12,6 +12,7 @@ using namespace std;
 #include "tests/test-instream.cpp"
 #include "tests/test-pattern.cpp"
 #include "tests/test-stringToLongLong.cpp"
+#include "tests/test-stringToUnsignedLongLong.cpp"
 
 int main() {
     disableFinalizeGuard();

+ 6 - 2
tests/test-004_use-test.h/tests/test-stringToLongLong.cpp

@@ -1,6 +1,6 @@
 TEST(stringToLongLong) {
-    ensure(stringToLongLong(inf, "123") == 123LL);
-    ensure(stringToLongLong(inf, "-123") == -123LL);
+    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);
@@ -10,10 +10,14 @@ TEST(stringToLongLong) {
     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");});
+}