xref: /isa-l_crypto/include/test.h (revision 8c5795a37a3feddcae0ae34deda2717566fd3984)
1 /**********************************************************************
2   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef _TEST_H
31 #define _TEST_H
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "endian_helper.h"
38 
39 #define DIM(_x) (sizeof(_x)/sizeof(_x[0]))
40 
41 #define CHECK_RETURN_GOTO(state, expected, func, label)	do{ \
42 	if((state) != (expected)){ \
43 		printf("test: %s() - expected return " \
44 		       "value %d, got %d\n", func, expected, state); \
45 		goto label; \
46 	} \
47 }while(0)
48 
49 // Use sys/time.h functions for time
50 #if defined (__unix__) || (__APPLE__) || (__MINGW32__)
51 # include <sys/time.h>
52 #endif
53 
54 #ifdef _MSC_VER
55 # define inline __inline
56 # include <time.h>
57 # include <Windows.h>
58 #endif
59 
60 #include <stdio.h>
61 #include <stdint.h>
62 
63 struct perf{
64 	struct timeval tv;
65 };
66 
67 
68 #if defined (__unix__) || (__APPLE__) || (__MINGW32__)
69 static inline int perf_start(struct perf *p)
70 {
71 	return gettimeofday(&(p->tv), 0);
72 }
73 static inline int perf_stop(struct perf *p)
74 {
75 	return gettimeofday(&(p->tv), 0);
76 }
77 
78 static inline void perf_print(struct perf stop, struct perf start, long long dsize)
79 {
80 	long long secs = stop.tv.tv_sec - start.tv.tv_sec;
81 	long long usecs = secs * 1000000 + stop.tv.tv_usec - start.tv.tv_usec;
82 
83 	printf("runtime = %10lld usecs", usecs);
84 	if (dsize != 0) {
85 #if 1 // not bug in printf for 32-bit
86 		printf(", bandwidth %lld MB in %.4f sec = %.2f MB/s\n", dsize/(1024*1024),
87 			((double) usecs)/1000000, ((double) dsize) / (double)usecs);
88 #else
89 		printf(", bandwidth %lld MB ", dsize/(1024*1024));
90 		printf("in %.4f sec ",(double)usecs/1000000);
91 		printf("= %.2f MB/s\n", (double)dsize/usecs);
92 #endif
93 	}
94 	else
95 		printf("\n");
96 }
97 #endif
98 
99 static inline uint64_t get_filesize(FILE *fp)
100 {
101 	uint64_t file_size;
102 	fpos_t pos, pos_curr;
103 
104 	fgetpos(fp, &pos_curr);  /* Save current position */
105 #if defined(_WIN32) || defined(_WIN64)
106 	_fseeki64(fp, 0, SEEK_END);
107 #else
108 	fseeko(fp, 0, SEEK_END);
109 #endif
110 	fgetpos(fp, &pos);
111 	file_size = *(uint64_t *)&pos;
112 	fsetpos(fp, &pos_curr);  /* Restore position */
113 
114 	return file_size;
115 }
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif // _TEST_H
122