From 13c8106f4f0ffb1b5568ce628296025dd8fb6ac1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 20 Feb 2009 06:10:44 +0000 Subject: [PATCH] - Add 'unit_libcbase', the beginnings of a unit test module for m_libcbase. - Rename 'oset_test' as 'unit_oset' to make its meaning more clear. - Remove VG_(atoll36), VG_(strtoll8)() and VG_(strtoll36)(); they're not used and so untested, but easy to crib from similar functions if they need to be added again later. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@9204 --- coregrind/m_libcbase.c | 77 +++---------------- include/pub_tool_libcbase.h | 17 ++-- memcheck/tests/Makefile.am | 10 ++- ...st.stderr.exp => unit_libcbase.stderr.exp} | 0 memcheck/tests/unit_libcbase.vgtest | 2 + memcheck/tests/{oset_test.c => unit_oset.c} | 1 - memcheck/tests/unit_oset.stderr.exp | 0 ...t_test.stdout.exp => unit_oset.stdout.exp} | 0 .../{oset_test.vgtest => unit_oset.vgtest} | 0 9 files changed, 28 insertions(+), 79 deletions(-) rename memcheck/tests/{oset_test.stderr.exp => unit_libcbase.stderr.exp} (100%) create mode 100644 memcheck/tests/unit_libcbase.vgtest rename memcheck/tests/{oset_test.c => unit_oset.c} (99%) create mode 100644 memcheck/tests/unit_oset.stderr.exp rename memcheck/tests/{oset_test.stdout.exp => unit_oset.stdout.exp} (100%) rename memcheck/tests/{oset_test.vgtest => unit_oset.vgtest} (100%) diff --git a/coregrind/m_libcbase.c b/coregrind/m_libcbase.c index e78536f232..cf983509e7 100644 --- a/coregrind/m_libcbase.c +++ b/coregrind/m_libcbase.c @@ -50,12 +50,6 @@ Bool VG_(isdigit) ( Char c ) Converting strings to numbers ------------------------------------------------------------------ */ -static Bool is_oct_digit(Char c, Long* digit) -{ - if (c >= '0' && c <= '7') { *digit = (Long)(c - '0'); return True; } - return False; -} - static Bool is_dec_digit(Char c, Long* digit) { if (c >= '0' && c <= '9') { *digit = (Long)(c - '0'); return True; } @@ -70,40 +64,11 @@ static Bool is_hex_digit(Char c, Long* digit) return False; } -static Bool is_base36_digit(Char c, Long* digit) -{ - if (c >= '0' && c <= '9') { *digit = (Long)(c - '0'); return True; } - if (c >= 'A' && c <= 'Z') { *digit = (Long)((c - 'A') + 10); return True; } - if (c >= 'a' && c <= 'z') { *digit = (Long)((c - 'a') + 10); return True; } - return False; -} - -Long VG_(strtoll8) ( Char* str, Char** endptr ) -{ - Bool neg = False; - Long n = 0, digit = 0; - - // Skip leading whitespace. - while (VG_(isspace)(*str)) str++; - - // Allow a leading '-' or '+'. - if (*str == '-') { str++; neg = True; } - else if (*str == '+') { str++; } - - while (is_oct_digit(*str, &digit)) { - n = 8*n + digit; - str++; - } - - if (neg) n = -n; - if (endptr) *endptr = str; // Record first failing character. - return n; -} - Long VG_(strtoll10) ( Char* str, Char** endptr ) { - Bool neg = False; + Bool neg = False, converted = False; Long n = 0, digit = 0; + Char* str0 = str; // Skip leading whitespace. while (VG_(isspace)(*str)) str++; @@ -113,19 +78,22 @@ Long VG_(strtoll10) ( Char* str, Char** endptr ) else if (*str == '+') { str++; } while (is_dec_digit(*str, &digit)) { + converted = True; // Ok, we've actually converted a digit. n = 10*n + digit; str++; } - if (neg) n = -n; + if (!converted) str = str0; // If nothing converted, endptr points to + if (neg) n = -n; // the start of the string. if (endptr) *endptr = str; // Record first failing character. return n; } Long VG_(strtoll16) ( Char* str, Char** endptr ) { - Bool neg = False; + Bool neg = False, converted = False; Long n = 0, digit = 0; + Char* str0 = str; // Skip leading whitespace. while (VG_(isspace)(*str)) str++; @@ -143,33 +111,13 @@ Long VG_(strtoll16) ( Char* str, Char** endptr ) } while (is_hex_digit(*str, &digit)) { + converted = True; // Ok, we've actually converted a digit. n = 16*n + digit; str++; } - if (neg) n = -n; - if (endptr) *endptr = str; // Record first failing character. - return n; -} - -Long VG_(strtoll36) ( Char* str, Char** endptr ) -{ - Bool neg = False; - Long n = 0, digit = 0; - - // Skip leading whitespace. - while (VG_(isspace)(*str)) str++; - - // Allow a leading '-' or '+'. - if (*str == '-') { str++; neg = True; } - else if (*str == '+') { str++; } - - while (is_base36_digit(*str, &digit)) { - n = 36*n + digit; - str++; - } - - if (neg) n = -n; + if (!converted) str = str0; // If nothing converted, endptr points to + if (neg) n = -n; // the start of the string. if (endptr) *endptr = str; // Record first failing character. return n; } @@ -217,11 +165,6 @@ Long VG_(atoll16) ( Char* str ) return VG_(strtoll16)(str, NULL); } -Long VG_(atoll36) ( Char* str ) -{ - return VG_(strtoll36)(str, NULL); -} - /* --------------------------------------------------------------------- String functions ------------------------------------------------------------------ */ diff --git a/include/pub_tool_libcbase.h b/include/pub_tool_libcbase.h index 53fd0f1613..bc224ac418 100644 --- a/include/pub_tool_libcbase.h +++ b/include/pub_tool_libcbase.h @@ -47,25 +47,28 @@ extern Bool VG_(isdigit) ( Char c ); // accepts an initial "0x" or "0X" prefix, but only if it's followed by a // hex digit (if not, the '0' will be read and then it will stop on the // "x"/"X".) If 'endptr' isn't NULL, it gets filled in with the first -// non-digit char. None of them test that the number fits into 64 bits. +// non-digit char. Returns 0 if no number could be converted, and 'endptr' +// is set to the start of the string. None of them test that the number +// fits into 64 bits. // // Nb: if you're wondering why we don't just have a single VG_(strtol) which // takes a base, it's because I wanted it to assert if it was given a bogus // base (the standard glibc one sets 'errno' in this case). But // m_libcbase.c doesn't import any code, not even vg_assert. --njn -extern Long VG_(strtoll8) ( Char* str, Char** endptr ); extern Long VG_(strtoll10) ( Char* str, Char** endptr ); extern Long VG_(strtoll16) ( Char* str, Char** endptr ); -extern Long VG_(strtoll36) ( Char* str, Char** endptr ); - // Convert a string to a double. After leading whitespace is ignored, - // it accepts a non-empty sequence of decimal digits possibly containing - // a '.'. + // Convert a string to a double. After leading whitespace is ignored, a + // '+' or '-' is allowed, and then it accepts a non-empty sequence of + // decimal digits possibly containing a '.'. Hexadecimal floats are not + // accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN"). extern double VG_(strtod) ( Char* str, Char** endptr ); + // These are just like their VG_(strtoll*) counterparts, except that you + // cannot tell if an error occurred (because 0 is returned) or if there + // is any trailing non-numeric characterws (eg. in "123xyz"). extern Long VG_(atoll) ( Char* str ); // base 10 extern Long VG_(atoll16) ( Char* str ); // base 16; leading 0x accepted -extern Long VG_(atoll36) ( Char* str ); // base 36 /* --------------------------------------------------------------------- String functions and macros diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am index d0fb56c02a..635ade0622 100644 --- a/memcheck/tests/Makefile.am +++ b/memcheck/tests/Makefile.am @@ -92,6 +92,7 @@ EXTRA_DIST = $(noinst_SCRIPTS) \ memcmptest.stderr.exp memcmptest.stderr.exp2 \ memcmptest.stdout.exp memcmptest.vgtest \ mempool.stderr.exp mempool.stderr.exp64 mempool.vgtest \ + metadata.stderr.exp metadata.stdout.exp metadata.vgtest \ mismatches.stderr.exp mismatches.stderr.exp64 mismatches.vgtest \ mmaptest.stderr.exp mmaptest.vgtest \ nanoleak.stderr.exp nanoleak.vgtest \ @@ -117,7 +118,6 @@ EXTRA_DIST = $(noinst_SCRIPTS) \ origin6-fp.vgtest origin6-fp.stdout.exp \ origin6-fp.stderr.exp-glibc25-amd64 \ origin6-fp.stderr.exp-glibc27-ppc64 \ - oset_test.stderr.exp oset_test.stdout.exp oset_test.vgtest \ overlap.stderr.exp overlap.stdout.exp overlap.vgtest \ partiallydefinedeq.vgtest partiallydefinedeq.stderr.exp \ partiallydefinedeq.stderr.exp2 \ @@ -151,7 +151,8 @@ EXTRA_DIST = $(noinst_SCRIPTS) \ suppfree.stderr.exp suppfree.vgtest \ toobig-allocs.stderr.exp toobig-allocs.vgtest \ trivialleak.stderr.exp trivialleak.vgtest \ - metadata.stderr.exp metadata.stdout.exp metadata.vgtest \ + unit_libcbase.stderr.exp unit_libcbase.stdout.exp unit_libcbase.vgtest \ + unit_oset.stderr.exp unit_oset.stdout.exp unit_oset.vgtest \ varinfo1.vgtest varinfo1.stdout.exp varinfo1.stderr.exp \ varinfo2.vgtest varinfo2.stdout.exp varinfo2.stderr.exp \ varinfo3.vgtest varinfo3.stdout.exp varinfo3.stderr.exp \ @@ -200,9 +201,10 @@ check_PROGRAMS = \ malloc_usable malloc1 malloc2 malloc3 manuel1 manuel2 manuel3 \ match-overrun \ memalign_test memalign2 memcmptest mempool mmaptest \ + mismatches new_override metadata \ nanoleak nanoleak2 new_nothrow \ noisy_child \ - null_socket oset_test \ + null_socket \ origin1-yes origin2-not-quite origin3-no \ origin4-many origin5-bz2 origin6-fp \ overlap \ @@ -216,7 +218,7 @@ check_PROGRAMS = \ stack_changes strchr str_tester \ supp_unknown supp1 supp2 suppfree \ trivialleak \ - mismatches new_override metadata \ + unit_libcbase unit_oset \ varinfo1 varinfo2 varinfo3 varinfo4 \ varinfo5 varinfo5so.so varinfo6 \ vcpu_fbench vcpu_fnfns \ diff --git a/memcheck/tests/oset_test.stderr.exp b/memcheck/tests/unit_libcbase.stderr.exp similarity index 100% rename from memcheck/tests/oset_test.stderr.exp rename to memcheck/tests/unit_libcbase.stderr.exp diff --git a/memcheck/tests/unit_libcbase.vgtest b/memcheck/tests/unit_libcbase.vgtest new file mode 100644 index 0000000000..190890439a --- /dev/null +++ b/memcheck/tests/unit_libcbase.vgtest @@ -0,0 +1,2 @@ +vgopts: -q +prog: unit_libcbase diff --git a/memcheck/tests/oset_test.c b/memcheck/tests/unit_oset.c similarity index 99% rename from memcheck/tests/oset_test.c rename to memcheck/tests/unit_oset.c index 10033a2dfa..f06f5826c6 100644 --- a/memcheck/tests/oset_test.c +++ b/memcheck/tests/unit_oset.c @@ -213,7 +213,6 @@ void example1b(void) Int i, n; Word v = 0, prev; Word vs[NN]; - Word *pv; // Create a static OSet of Ints. This one uses fast (built-in) // comparisons. diff --git a/memcheck/tests/unit_oset.stderr.exp b/memcheck/tests/unit_oset.stderr.exp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/memcheck/tests/oset_test.stdout.exp b/memcheck/tests/unit_oset.stdout.exp similarity index 100% rename from memcheck/tests/oset_test.stdout.exp rename to memcheck/tests/unit_oset.stdout.exp diff --git a/memcheck/tests/oset_test.vgtest b/memcheck/tests/unit_oset.vgtest similarity index 100% rename from memcheck/tests/oset_test.vgtest rename to memcheck/tests/unit_oset.vgtest -- 2.47.3