1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Unit tests for basic features in Blosc.
5
6 Creation date: 2010-06-07
7 Author: Francesc Alted <francesc@blosc.org>
8
9 See LICENSES/BLOSC.txt for details about copyright and rights to use.
10 **********************************************************************/
11
12 #ifndef BLOSC_TEST_COMMON_H
13 #define BLOSC_TEST_COMMON_H
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #if defined(_WIN32) && !defined(__MINGW32__)
22 #include <time.h>
23 #include "win32/stdint-windows.h"
24 #else
25 #include <stdint.h>
26 #include <unistd.h>
27 #include <sys/time.h>
28 #endif
29 #include <math.h>
30 #include "../blosc/blosc.h"
31
32 #if defined(_WIN32)
33 /* MSVC and MinGW do not have setenv */
34 #define setenv(name, value, overwrite) do {_putenv_s(name, value);} while(0)
35 #endif
36
37
38 /* This is MinUnit in action (http://www.jera.com/techinfo/jtns/jtn002.html) */
39 #define mu_assert(message, test) do { if (!(test)) return message; } while (0)
40 #define mu_run_test(test) do \
41 { const char *message = test(); tests_run++; \
42 if (message) { printf("%c", 'F'); return message;} \
43 else printf("%c", '.'); } while (0)
44
45 extern int tests_run;
46
47 #define KB 1024
48 #define MB (1024*KB)
49 #define GB (1024*MB)
50
51 /*
52 Memory functions.
53 */
54
55 /** Allocates a block of memory with the specified size and alignment.
56 The allocated memory is 'cleaned' before returning to avoid
57 accidental re-use of data within or between tests.
58 */
blosc_test_malloc(const size_t alignment,const size_t size)59 static void* blosc_test_malloc(const size_t alignment, const size_t size)
60 {
61 const int32_t clean_value = 0x99;
62 void *block = NULL;
63 int32_t res = 0;
64
65 #if defined(_ISOC11_SOURCE) || (defined(__FreeBSD__) && __STDC_VERSION__ >= 201112L)
66 /* C11 aligned allocation. 'size' must be a multiple of the alignment. */
67 block = aligned_alloc(alignment, size);
68 #elif defined(_WIN32)
69 /* A (void *) cast needed for avoiding a warning with MINGW :-/ */
70 block = (void *)_aligned_malloc(size, alignment);
71 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
72 /* Platform does have an implementation of posix_memalign */
73 res = posix_memalign(&block, alignment, size);
74 #elif defined(__APPLE__)
75 /* Mac OS X guarantees 16-byte alignment in small allocs */
76 block = malloc(size);
77 #else
78 #error Cannot determine how to allocate aligned memory on the target platform.
79 #endif
80
81 if (block == NULL || res != 0) {
82 fprintf(stderr, "Error allocating memory!");
83 return NULL;
84 }
85
86 /* Clean the allocated memory before returning. */
87 memset(block, clean_value, size);
88
89 return block;
90 }
91
92 /** Frees memory allocated by blosc_test_malloc. */
blosc_test_free(void * ptr)93 static void blosc_test_free(void* ptr)
94 {
95 #if defined(_WIN32)
96 _aligned_free(ptr);
97 #else
98 free(ptr);
99 #endif /* _WIN32 */
100 }
101
102 /** Fills a buffer with random values. */
blosc_test_fill_random(void * const ptr,const size_t size)103 static void blosc_test_fill_random(void* const ptr, const size_t size)
104 {
105 size_t k;
106 uint8_t* const byte_ptr = (uint8_t*)ptr;
107 for (k = 0; k < size; k++) {
108 byte_ptr[k] = rand();
109 }
110 }
111
112 /*
113 Argument parsing.
114 */
115
116 /** Parse a `int32_t` value from a string, checking for overflow. */
blosc_test_parse_uint32_t(const char * const str,uint32_t * value)117 static int blosc_test_parse_uint32_t(const char* const str, uint32_t* value)
118 {
119 char* str_end;
120 int32_t signed_value = strtol(str, &str_end, 10);
121 if (signed_value < 0 || *str_end)
122 {
123 return 0;
124 }
125 else
126 {
127 *value = (uint32_t)signed_value;
128 return 1;
129 }
130 }
131
132 /*
133 Error message functions.
134 */
135
136 /** Print an error message when a test program has been invoked
137 with an invalid number of arguments. */
blosc_test_print_bad_argcount_msg(const int32_t num_expected_args,const int32_t num_actual_args)138 static void blosc_test_print_bad_argcount_msg(
139 const int32_t num_expected_args, const int32_t num_actual_args)
140 {
141 fprintf(stderr, "Invalid number of arguments specified.\nExpected %d arguments but was given %d.",
142 num_expected_args, num_actual_args);
143 }
144
145 /** Print an error message when a test program has been invoked
146 with an invalid argument value. */
blosc_test_print_bad_arg_msg(const int32_t arg_index)147 static void blosc_test_print_bad_arg_msg(const int32_t arg_index)
148 {
149 fprintf(stderr, "Invalid value specified for argument at index %d.\n", arg_index);
150 }
151
152 #endif /* !defined(BLOSC_TEST_COMMON_H) */
153