From 90560d7194f3e451ddab9d4033c98d2e6aec977b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 23 Aug 2015 10:19:23 -0700 Subject: 2062 --- html/002test.cc.html | 61 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'html/002test.cc.html') diff --git a/html/002test.cc.html b/html/002test.cc.html index 52b32d33..d6d5ece6 100644 --- a/html/002test.cc.html +++ b/html/002test.cc.html @@ -13,14 +13,13 @@ pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-family: monospace; color: #eeeeee; background-color: #080808; } * { font-size: 1.05em; } -.Constant { color: #00a0a0; } .cSpecial { color: #008000; } -.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; } -.Comment { color: #9090ff; } -.Delimiter { color: #a04060; } .CommentedCode { color: #6c6c6c; } .PreProc { color: #c000c0; } +.Comment { color: #9090ff; } +.Delimiter { color: #a04060; } .Identifier { color: #804000; } +.Constant { color: #00a0a0; } --> @@ -43,19 +42,19 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; } //: subsets of the program. :(before "End Types") -typedef void (*test_fn)(void); +typedef void (*test_fn)(void); :(before "End Globals") -const test_fn Tests[] = { +const test_fn Tests[] = { #include "test_list" // auto-generated; see makefile }; -bool Run_tests = false; -bool Passed = true; // set this to false inside any test to indicate failure -long Num_failures = 0; +bool Run_tests = false; +bool Passed = true; // set this to false inside any test to indicate failure +long Num_failures = 0; #define CHECK(X) \ - if (!(X)) { \ + if (!(X)) { \ ++Num_failures; \ cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \ Passed = false; \ @@ -63,7 +62,7 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; } } #define CHECK_EQ(X, Y) \ - if ((X) != (Y)) { \ + if ((X) != (Y)) { \ ++Num_failures; \ cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << " == " << #Y << '\n'; \ cerr << " got " << (X) << '\n'; /* BEWARE: multiple eval */ \ @@ -75,25 +74,25 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; } Passed = true; :(before "End Commandline Parsing") -if (argc > 1 && is_equal(argv[1], "test")) { +if (argc > 1 && is_equal(argv[1], "test")) { Run_tests = true; --argc; ++argv; // shift 'test' out of commandline args } :(before "End Main") -if (Run_tests) { +if (Run_tests) { // Test Runs // we run some tests and then exit; assume no state need be maintained afterward // End Test Run Initialization - time_t t; time(&t); + time_t t; time(&t); cerr << "C tests: " << ctime(&t); - for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) { + for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) { //? cerr << i << '\n'; //? 2 run_test(i); } // End Tests cerr << '\n'; - if (Num_failures > 0) { + if (Num_failures > 0) { cerr << Num_failures << " failure" << (Num_failures > 1 ? "s" : "") << '\n'; @@ -103,8 +102,8 @@ Passed = true; } :(code) -void run_test(size_t i) { - if (i >= sizeof(Tests)/sizeof(Tests[0])) { +void run_test(size_t i) { + if (i >= sizeof(Tests)/sizeof(Tests[0])) { cerr << "no test " << i << '\n'; return; } @@ -112,22 +111,34 @@ Passed = true; // End Test Setup (*Tests[i])(); teardown(); - if (Passed) cerr << "."; + if (Passed) cerr << "."; } -bool is_integer(const string& s) { - return s.find_first_not_of("0123456789-") == string::npos; +bool is_integer(const string& s) { + return s.find_first_not_of("0123456789-") == string::npos + && s.find('-', 1) == string::npos + && s.find_first_of("0123456789") != string::npos; } -long long int to_integer(string n) { - char* end = NULL; +long long int to_integer(string n) { + char* end = NULL; // safe because string.c_str() is guaranteed to be null-terminated - long long int result = strtoll(n.c_str(), &end, /*any base*/0); - if (*end != '\0') cerr << "tried to convert " << n << " to number\n"; + long long int result = strtoll(n.c_str(), &end, /*any base*/0); + if (*end != '\0') cerr << "tried to convert " << n << " to number\n"; assert(*end == '\0'); return result; } +void test_is_integer() { + CHECK(is_integer("1234")); + CHECK(is_integer("-1")); + CHECK(!is_integer("234.0")); + CHECK(is_integer("-567")); + CHECK(!is_integer("89-0")); + CHECK(!is_integer("-")); + CHECK(!is_integer("1e3")); // not supported +} + :(before "End Includes") #include<cstdlib> -- cgit 1.4.1-2-gfad0