Browse Source

Merge pull request #181 from libtom/pr/demo-test-facelift

karel-m 8 years ago
parent
commit
0a400f465f
4 changed files with 59 additions and 10 deletions
  1. 30 8
      demos/test.c
  2. 1 2
      testprof/katja_test.c
  3. 1 0
      testprof/tomcrypt_test.h
  4. 27 0
      testprof/x86_prof.c

+ 30 - 8
demos/test.c

@@ -26,10 +26,12 @@ static const struct {
       LTC_TEST_FN(katja_test),
 };
 
-int main(void)
+int main(int argc, char **argv)
 {
-   int x;
+   int x, pass = 0, fail = 0, nop = 0;
    size_t fn_len, i, dots;
+   char *single_test = NULL;
+   ulong64 ts, dur = 0;
    reg_algs();
 
    printf("build == \n%s\n", crypt_build_settings);
@@ -58,26 +60,46 @@ int main(void)
 
    fn_len = fn_len + (4 - (fn_len % 4));
 
+   /* single test name from commandline */
+   if (argc > 1) single_test = argv[1];
+
    for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
+      if (single_test && strcmp(test_functions[i].name, single_test)) {
+        continue;
+      }
       dots = fn_len - strlen(test_functions[i].name);
 
       printf("\n%s", test_functions[i].name);
       while(dots--) printf(".");
       fflush(stdout);
 
+      ts = epoch_usec();
       x = test_functions[i].fn();
+      ts = epoch_usec() - ts;
+      dur += ts;
 
-      if (x) {
-         printf("failed\n");
-         exit(EXIT_FAILURE);
+      if (x == CRYPT_OK) {
+         printf("passed %10.3fms", (double)(ts)/1000);
+         pass++;
+      }
+      else if (x == CRYPT_NOP) {
+         printf("nop");
+         nop++;
       }
       else {
-         printf("passed");
+         printf("failed %10.3fms", (double)(ts)/1000);
+         fail++;
       }
    }
 
-   printf("\n");
-   return EXIT_SUCCESS;
+   if (fail > 0 || fail+pass+nop == 0) {
+      printf("\n\nFAILURE: passed=%d failed=%d nop=%d duration=%.1fsec\n", pass, fail, nop, (double)(dur)/(1000*1000));
+      return EXIT_FAILURE;
+   }
+   else {
+      printf("\n\nSUCCESS: passed=%d failed=%d nop=%d duration=%.1fsec\n", pass, fail, nop, (double)(dur)/(1000*1000));
+      return EXIT_SUCCESS;
+   }
 }
 
 /* $Source$ */

+ 1 - 2
testprof/katja_test.c

@@ -224,8 +224,7 @@ for (cnt = 0; cnt < len; ) {
 
 int katja_test(void)
 {
-   fprintf(stderr, "NOP");
-   return 0;
+   return CRYPT_NOP;
 }
 
 #endif

+ 1 - 0
testprof/tomcrypt_test.h

@@ -88,6 +88,7 @@ int compare_testvector(const void* is, const unsigned long is_len, const void* s
 int sorter(const void *a, const void *b);
 void tally_results(int type);
 ulong64 rdtsc (void);
+ulong64 epoch_usec(void);
 
 void t_start(void);
 ulong64 t_read(void);

+ 27 - 0
testprof/x86_prof.c

@@ -1,5 +1,32 @@
 #include <tomcrypt_test.h>
 
+#if defined(_WIN32)
+  #include <windows.h> /* GetSystemTimeAsFileTime */
+#else
+  #include <sys/time.h>
+#endif
+
+/* microseconds since 1970 (UNIX epoch) */
+ulong64 epoch_usec(void)
+{
+#if defined(LTC_NO_TEST_TIMING)
+  return 0;
+#elif defined(_WIN32)
+  FILETIME CurrentTime;
+  ulong64 cur_time;
+  GetSystemTimeAsFileTime(&CurrentTime);
+  cur_time = ((ulong64)CurrentTime.dwHighDateTime << 32) + (ulong64)CurrentTime.dwLowDateTime;
+  cur_time -= 116444736000000000LL; /* subtract epoch in microseconds */
+  cur_time /= 10; /* nanoseconds > microseconds */
+  return cur_time;
+#else
+  struct timeval tv;
+  struct timezone tz;
+  gettimeofday(&tv, &tz);
+  return (ulong64)(tv.tv_sec) * 1000000 + (ulong64)(tv.tv_usec); /* get microseconds */
+#endif
+}
+
 prng_state yarrow_prng;
 
 void print_hex(const char* what, const void* v, const unsigned long l)