1 /*
2  *  libzvbi - Unit test helper functions
3  *
4  *  Copyright (C) 2007 Michael H. Schimek
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA 02110-1301, USA.
20  */
21 
22 /* $Id: test-common.cc,v 1.7 2009/03/04 21:48:14 mschimek Exp $ */
23 
24 #undef NDEBUG
25 
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29 
30 #include <stdlib.h>
31 
32 #include "src/misc.h"
33 #include "test-common.h"
34 #include "src/version.h"
35 
36 void *
memset_rand(void * dst,size_t n_bytes)37 memset_rand			(void *			dst,
38 				 size_t			n_bytes)
39 {
40 	uint8_t *p;
41 	uint8_t *p_end;
42 	unsigned int x;
43 	size_t todo;
44 
45 	assert (NULL != dst);
46 	assert (n_bytes > 0);
47 	assert (n_bytes < (10 << 20));
48 
49 	p = (uint8_t *) dst;
50 
51 	todo = (size_t) dst & 3;
52 	if (0 != todo) {
53 		todo = MIN (4 - todo, n_bytes);
54 		p_end = p + todo;
55 		x = mrand48 ();
56 
57 		while (p < p_end) {
58 			*p++ = x;
59 			x >>= 8;
60 		}
61 
62 		n_bytes -= todo;
63 	}
64 
65 	p_end = p + (n_bytes & ~3);
66 
67 	for (; p < p_end; p += 4)
68 		* ((uint32_t *) p) = mrand48 ();
69 
70 	todo = n_bytes & 3;
71 	if (0 != todo) {
72 		p_end = p + todo;
73 		x = mrand48 ();
74 
75 		while (p < p_end) {
76 			*p++ = x;
77 			x >>= 8;
78 		}
79 	}
80 
81 	return dst;
82 }
83 
84 int
memcmp_zero(const void * src,size_t n_bytes)85 memcmp_zero			(const void *		src,
86 				 size_t			n_bytes)
87 {
88 	const uint8_t *s;
89 
90 	for (s = (const uint8_t *) src; n_bytes > 0; ++s, --n_bytes) {
91 		if (0 != *s)
92 			return 1;
93 	}
94 
95 	return 0;
96 }
97 
98 void *
xmalloc(size_t n_bytes)99 xmalloc				(size_t			n_bytes)
100 {
101 	void *p;
102 
103 	assert (n_bytes > 0);
104 	assert (n_bytes < (10 << 20));
105 
106 	p = malloc (n_bytes);
107 	assert (NULL != p);
108 
109 	return p;
110 }
111 
112 void *
xralloc(size_t n_bytes)113 xralloc				(size_t			n_bytes)
114 {
115 	return memset_rand (xmalloc (n_bytes), n_bytes);
116 }
117 
118 void *
xmemdup(const void * src,size_t n_bytes)119 xmemdup				(const void *		src,
120 				 size_t			n_bytes)
121 {
122 	void *dst;
123 
124 	assert (NULL != src);
125 
126 	dst = xmalloc (n_bytes);
127 	memcpy (dst, src, n_bytes);
128 
129 	return dst;
130 }
131 
132 #if 3 == VBI_VERSION_MINOR
133 static unsigned int		malloc_count;
134 static unsigned int		malloc_fail_cycle;
135 
136 static void *
my_malloc(size_t n_bytes)137 my_malloc			(size_t			n_bytes)
138 {
139 	if (malloc_count++ == malloc_fail_cycle)
140 		return NULL;
141 	else
142 		return malloc (n_bytes);
143 }
144 #endif
145 
146 void
test_malloc(void (* function)(void),unsigned int n_cycles)147 test_malloc			(void			(* function)(void),
148 				 unsigned int		n_cycles)
149 {
150 #if 3 == VBI_VERSION_MINOR
151 	vbi_malloc = my_malloc;
152 
153 	for (malloc_fail_cycle = 0; malloc_fail_cycle < n_cycles;
154 	     ++malloc_fail_cycle) {
155 		malloc_count = 0;
156 		function ();
157 	}
158 
159 	vbi_malloc = malloc;
160 #else
161 	function = function; /* unused */
162 	n_cycles = n_cycles;
163 #endif
164 }
165 
166 /*
167 Local variables:
168 c-set-style: K&R
169 c-basic-offset: 8
170 End:
171 */
172