]> git.ozlabs.org Git - ccan/commitdiff
sha256: Simplify test
authorJon Griffiths <jon_p_griffiths@yahoo.com>
Sun, 28 Aug 2016 23:52:44 +0000 (11:52 +1200)
committerJon Griffiths <jon_p_griffiths@yahoo.com>
Mon, 29 Aug 2016 00:10:00 +0000 (12:10 +1200)
We can use the iteration count in the test case to determine
whether a single call is required. This simplifies the code and
also means that we don't overstate the actual number of tests
performed by a factor of 2. Simplify a couple of expressions
while we are changing this.

ccan/crypto/sha256/test/run-test-vectors.c

index a64c35e468698632cf897e225834e02f2d0d5aef..ed2eb5f96dcf3ad81a7d4cb3fea557042181ddab 100644 (file)
@@ -45,15 +45,13 @@ static struct test tests[] = {
 #endif
 };
 
 #endif
 };
 
-static bool do_test(const struct test *t, bool single)
+static bool do_test(const struct test *t)
 {
        struct sha256 h;
 
 {
        struct sha256 h;
 
-       if (single) {
-               if (t->repetitions != 1)
-                       return true;
+       if (t->repetitions == 1)
                sha256(&h, t->test, strlen(t->test));
                sha256(&h, t->test, strlen(t->test));
-       else {
+       else {
                struct sha256_ctx ctx = SHA256_INIT;
                size_t i;
 
                struct sha256_ctx ctx = SHA256_INIT;
                size_t i;
 
@@ -62,7 +60,7 @@ static bool do_test(const struct test *t, bool single)
                sha256_done(&ctx, &h);
        }
 
                sha256_done(&ctx, &h);
        }
 
-       return memcmp(&h.u, t->result, sizeof(t->result)) == 0;
+       return memcmp(&h, t->result, sizeof(h)) == 0;
 }
 
 int main(void)
 }
 
 int main(void)
@@ -70,13 +68,10 @@ int main(void)
        size_t i;
 
        /* This is how many tests you plan to run */
        size_t i;
 
        /* This is how many tests you plan to run */
-       plan_tests(sizeof(tests) / sizeof(struct test) * 2);
+       plan_tests(sizeof(tests) / sizeof(tests[0]));
 
 
-       for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
-               ok1(do_test(&tests[i], false));
-
-       for (i = 0; i < sizeof(tests) / sizeof(struct test); i++)
-               ok1(do_test(&tests[i], true));
+       for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
+               ok1(do_test(&tests[i]));
 
        /* This exits depending on whether all tests passed */
        return exit_status();
 
        /* This exits depending on whether all tests passed */
        return exit_status();