Browse Source

Run BFGMiner's unit tests for 'make check', and have --unittest exit with failure if any problems occur

Luke Dashjr 11 years ago
parent
commit
e03c6f012b
7 changed files with 61 additions and 0 deletions
  1. 7 0
      Makefile.am
  2. 4 0
      driver-aan.c
  3. 22 0
      miner.c
  4. 1 0
      miner.h
  5. 3 0
      scrypt.c
  6. 3 0
      test-bfgminer.sh
  7. 21 0
      util.c

+ 7 - 0
Makefile.am

@@ -83,6 +83,13 @@ bfgminer_SOURCES += miner.h compat.h  \
 		   sha2.c sha2.h api.c
 EXTRA_bfgminer_DEPENDENCIES =
 
+TESTS = test-bfgminer.sh
+EXTRA_DIST += test-bfgminer.sh
+SH_LOG_COMPILER = /bin/sh
+AM_TESTS_ENVIRONMENT = PATH='$(srcdir)':"$$PATH"; export PATH;
+TESTS_ENVIRONMENT = $(AM_TESTS_ENVIRONMENT)
+TEST_EXTENSIONS = .sh
+
 .PHONY: update-version
 update-version:
 	./gen-version.sh >version.h.new

+ 4 - 0
driver-aan.c

@@ -88,7 +88,10 @@ void _test_aan_pll(const unsigned expect, const uint8_t postdiv, const uint8_t p
 	const uint16_t pll = (((postdiv << 5) | prediv) << 9) | fbdiv;
 	const unsigned got = aan_pll2freq(pll);
 	if (got != expect)
+	{
+		++unittest_failures;
 		applog(LOG_WARNING, "%s test failed for %4u(%x,%02x,%3d): got %4u", "aan_pll2freq", expect, postdiv, prediv, fbdiv, got);
+	}
 }
 
 static
@@ -98,6 +101,7 @@ void _test_aan_pll2(const unsigned freq)
 	const unsigned got = aan_pll2freq(pll);
 	if (got / 12 != freq / 12)
 	{
+		++unittest_failures;
 		const uint8_t postdiv = (pll >> 0xe);
 		const uint8_t prediv = (pll >> 9) & 0x1f;
 		const uint16_t fbdiv = pll & 0x1ff;

+ 22 - 0
miner.c

@@ -168,6 +168,7 @@ int opt_expiry = 120;
 int opt_expiry_lp = 3600;
 unsigned long long global_hashrate;
 static bool opt_unittest = false;
+unsigned unittest_failures;
 unsigned long global_quota_gcd = 1;
 time_t last_getwork;
 
@@ -760,11 +761,17 @@ invsyntax:
 
 #define TEST_CGPU_MATCH(pattern)  \
 	if (!cgpu_match(pattern, &cgpu))  \
+	{  \
+		++unittest_failures;  \
 		applog(LOG_ERR, "%s: Pattern \"%s\" should have matched!", __func__, pattern);  \
+	}  \
 // END TEST_CGPU_MATCH
 #define TEST_CGPU_NOMATCH(pattern)  \
 	if (cgpu_match(pattern, &cgpu))  \
+	{  \
+		++unittest_failures;  \
 		applog(LOG_ERR, "%s: Pattern \"%s\" should NOT have matched!", __func__, pattern);  \
+	}  \
 // END TEST_CGPU_MATCH
 static __maybe_unused
 void test_cgpu_match()
@@ -1362,10 +1369,16 @@ void _test_intrange(const char *s, const int v[2])
 {
 	int a[2];
 	if (!get_intrange(s, &a[0], &a[1]))
+	{
+		++unittest_failures;
 		applog(LOG_ERR, "Test \"%s\" failed: returned false", s);
+	}
 	for (int i = 0; i < 2; ++i)
 		if (unlikely(a[i] != v[i]))
+		{
+			++unittest_failures;
 			applog(LOG_ERR, "Test \"%s\" failed: value %d should be %d but got %d", s, i, v[i], a[i]);
+		}
 }
 #define _test_intrange(s, ...)  _test_intrange(s, (int[]){ __VA_ARGS__ })
 
@@ -1374,7 +1387,10 @@ void _test_intrange_fail(const char *s)
 {
 	int a[2];
 	if (get_intrange(s, &a[0], &a[1]))
+	{
+		++unittest_failures;
 		applog(LOG_ERR, "Test !\"%s\" failed: returned true with %d and %d", s, a[0], a[1]);
+	}
 }
 
 static
@@ -3702,6 +3718,7 @@ void test_decimal_width()
 		percentf3(testbuf2, sizeof(testbuf2), testn, 10.0);
 		width = snprintf(printbuf, sizeof(printbuf), "%10g %s %s |", testn, testbuf1, testbuf2);
 		if (unlikely((saved != -1) && (width != saved))) {
+			++unittest_failures;
 			applog(LOG_ERR, "Test width mismatch in percentf3! %d not %d at %10g", width, saved, testn);
 			applog(LOG_ERR, "%s", printbuf);
 		}
@@ -3717,6 +3734,7 @@ void test_decimal_width()
 		snprintf(printbuf, sizeof(printbuf), "%10g %s %s %s |", testn, testbuf1, testbuf2, testbuf3);
 		width = utf8_strlen(printbuf);
 		if (unlikely((saved != -1) && (width != saved))) {
+			++unittest_failures;
 			applog(LOG_ERR, "Test width mismatch in format_unit2! %d not %d at %10g", width, saved, testn);
 			applog(LOG_ERR, "%s", printbuf);
 		}
@@ -3733,6 +3751,7 @@ void test_decimal_width()
 		snprintf(printbuf, sizeof(printbuf), "%10g %s %s %s %s |", testn, testbuf1, testbuf2, testbuf3, testbuf4);
 		width = utf8_strlen(printbuf);
 		if (unlikely((saved != -1) && (width != saved))) {
+			++unittest_failures;
 			applog(LOG_ERR, "Test width mismatch in pick_unit! %d not %d at %10g", width, saved, testn);
 			applog(LOG_ERR, "%s", printbuf);
 		}
@@ -9489,6 +9508,7 @@ void _test_target(void * const funcp, const char * const funcname, const bool li
 	if (memcmp(&buf[off], &expect[off], 4))
 	{
 testfail: ;
+		++unittest_failures;
 		char hexbuf[65], expectbuf[65];
 		bin2hex(hexbuf, buf, 32);
 		bin2hex(expectbuf, expect, 32);
@@ -12706,6 +12726,8 @@ int main(int argc, char *argv[])
 #ifdef USE_JINGTIAN
 		test_aan_pll();
 #endif
+		if (unittest_failures)
+			quit(1, "Unit tests failed");
 	}
 
 #ifdef HAVE_CURSES

+ 1 - 0
miner.h

@@ -1165,6 +1165,7 @@ extern int opt_hysteresis;
 extern int opt_fail_pause;
 extern int opt_log_interval;
 extern unsigned long long global_hashrate;
+extern unsigned unittest_failures;
 extern char *current_fullhash;
 extern double current_diff;
 extern double best_diff;

+ 3 - 0
scrypt.c

@@ -436,6 +436,7 @@ void test_scrypt(void)
 		};
 		if (memcmp(expect_X, X, sizeof(expect_X)))
 		{
+			++unittest_failures;
 			bin2hex32(hex, X, 32);
 			applog(LOG_ERR, "%s: %s failed (got %s)", __func__, "PBKDF2_SHA256_80_128", hex);
 		}
@@ -452,6 +453,7 @@ void test_scrypt(void)
 		};
 		if (memcmp(expect_X, X, sizeof(expect_X)))
 		{
+			++unittest_failures;
 			bin2hex32(hex, X, 16);
 			applog(LOG_ERR, "%s; %s failed (got %s)", __func__, "salsa20_8", hex);
 		}
@@ -465,6 +467,7 @@ void test_scrypt(void)
 		};
 		if (memcmp(expect_X, X, sizeof(expect_X)))
 		{
+			++unittest_failures;
 			bin2hex32(hex, X, 8);
 			applog(LOG_ERR, "%s: %s failed (got %s)", __func__, "scrypt_1024_1_1_256_sp", hex);
 		}

+ 3 - 0
test-bfgminer.sh

@@ -0,0 +1,3 @@
+#!/bin/sh
+echo $PATH
+bfgminer --unittest --no-default-config --scan noauto -d?

+ 21 - 0
util.c

@@ -1521,16 +1521,25 @@ void _utf8_test(const char *s, const wchar_t expected, int expectedlen)
 	{
 		len = utf8_len(((uint8_t*)s)[0]);
 		if (len != expectedlen)
+		{
+			++unittest_failures;
 			applog(LOG_ERR, "UTF-8 test U+%06lX (len %d) failed: got utf8_len=>%d", (unsigned long)expected, expectedlen, len);
+		}
 		len = utf8_strlen(s);
 		if (len != (s[0] ? 1 : 0))
+		{
+			++unittest_failures;
 			applog(LOG_ERR, "UTF-8 test U+%06lX (len %d) failed: got utf8_strlen=>%d", (unsigned long)expected, expectedlen, len);
+		}
 		len = -1;
 	}
 	
 	r = utf8_decode(s, &len);
 	if (unlikely(r != expected || expectedlen != len))
+	{
+		++unittest_failures;
 		applog(LOG_ERR, "UTF-8 test U+%06lX (len %d) failed: got U+%06lX (len %d)", (unsigned long)expected, expectedlen, (unsigned long)r, len);
+	}
 }
 #define _test_intrange(s, ...)  _test_intrange(s, (int[]){ __VA_ARGS__ })
 
@@ -2127,8 +2136,11 @@ void _test_extract_domain(const char * const expect, const char * const uri)
 	size_t sz;
 	const char * const d = extract_domain(&sz, uri, strlen(uri));
 	if (sz != strlen(expect) || strncasecmp(d, expect, sz))
+	{
+		++unittest_failures;
 		applog(LOG_WARNING, "extract_domain \"%s\" test failed; got \"%.*s\" instead of \"%s\"",
 		       uri, (int)sz, d, expect);
+	}
 }
 
 static
@@ -2137,8 +2149,11 @@ void _test_get_regd_domain(const char * const expect, const char * const fqdn)
 	size_t sz;
 	const char * const d = get_registered_domain(&sz, fqdn, strlen(fqdn));
 	if (d == NULL || sz != strlen(expect) || strncasecmp(d, expect, sz))
+	{
+		++unittest_failures;
 		applog(LOG_WARNING, "get_registered_domain \"%s\" test failed; got \"%.*s\" instead of \"%s\"",
 		       fqdn, (int)sz, d, expect);
+	}
 }
 
 void test_domain_funcs()
@@ -2297,10 +2312,13 @@ void _test_uri_find_param(const char * const uri, const char * const param, cons
 		actual_offset = actual - uri;
 	int actual_invert = (expect_invert >= 0) ? (invert ? 1 : 0) : -1;
 	if (actual_offset != expect_offset || expect_invert != actual_invert)
+	{
+		++unittest_failures;
 		applog(LOG_WARNING, "%s(\"%s\", \"%s\", %s) test failed (offset: expect=%d actual=%d; invert: expect=%d actual=%d)",
 		       "uri_find_param", uri, param, (expect_invert >= 0) ? "(invert)" : "NULL",
 		       expect_offset, actual_offset,
 		       expect_invert, actual_invert);
+	}
 }
 
 static
@@ -2308,8 +2326,11 @@ void _test_uri_get_param(const char * const uri, const char * const param, const
 {
 	const bool actual = uri_get_param_bool(uri, param, defval);
 	if (actual != expect)
+	{
+		++unittest_failures;
 		applog(LOG_WARNING, "%s(\"%s\", \"%s\", %s) test failed",
 		       "uri_get_param_bool", uri, param, defval ? "true" : "false");
+	}
 }
 
 void test_uri_get_param()