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) \
42 do { \
43 if ((state) != (expected)) { \
44 printf("test: %s() - expected return " \
45 "value %d, got %d\n", \
46 func, expected, state); \
47 goto label; \
48 } \
49 } while (0)
50
51 // Use sys/time.h functions for time
52 #if defined(__unix__) || (__MINGW32__)
53 #include <sys/time.h>
54 #endif
55
56 #ifdef _MSC_VER
57 #define inline __inline
58 #include <time.h>
59 #include <Windows.h>
60 #endif
61
62 #include <stdio.h>
63 #include <stdint.h>
64
65 struct perf {
66 struct timeval tv;
67 };
68
69 #if defined(__unix__) || (__MINGW32__)
70 static inline int
perf_start(struct perf * p)71 perf_start(struct perf *p)
72 {
73 return gettimeofday(&(p->tv), 0);
74 }
75 static inline int
perf_stop(struct perf * p)76 perf_stop(struct perf *p)
77 {
78 return gettimeofday(&(p->tv), 0);
79 }
80
81 static inline void
perf_print(struct perf stop,struct perf start,long long dsize)82 perf_print(struct perf stop, struct perf start, long long dsize)
83 {
84 long long secs = stop.tv.tv_sec - start.tv.tv_sec;
85 long long usecs = secs * 1000000 + stop.tv.tv_usec - start.tv.tv_usec;
86
87 printf("runtime = %10lld usecs", usecs);
88 if (dsize != 0) {
89 #if 1 // not bug in printf for 32-bit
90 printf(", bandwidth %lld MB in %.4f sec = %.2f MB/s\n", dsize / (1024 * 1024),
91 ((double) usecs) / 1000000, ((double) dsize) / (double) usecs);
92 #else
93 printf(", bandwidth %lld MB ", dsize / (1024 * 1024));
94 printf("in %.4f sec ", (double) usecs / 1000000);
95 printf("= %.2f MB/s\n", (double) dsize / usecs);
96 #endif
97 } else
98 printf("\n");
99 }
100 #endif
101
102 static inline uint64_t
get_filesize(FILE * fp)103 get_filesize(FILE *fp)
104 {
105 uint64_t file_size;
106 fpos_t pos, pos_curr;
107
108 fgetpos(fp, &pos_curr); /* Save current position */
109 #if defined(_WIN32) || defined(_WIN64)
110 _fseeki64(fp, 0, SEEK_END);
111 #else
112 fseeko(fp, 0, SEEK_END);
113 #endif
114 fgetpos(fp, &pos);
115 file_size = *(uint64_t *) &pos;
116 fsetpos(fp, &pos_curr); /* Restore position */
117
118 return file_size;
119 }
120
121 #ifdef __cplusplus
122 }
123 #endif
124
125 #endif // _TEST_H
126