diff options
Diffstat (limited to 'xdelta3/xdelta3-test.h')
-rw-r--r-- | xdelta3/xdelta3-test.h | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/xdelta3/xdelta3-test.h b/xdelta3/xdelta3-test.h index 02c440a..c34aa3f 100644 --- a/xdelta3/xdelta3-test.h +++ b/xdelta3/xdelta3-test.h | |||
@@ -31,7 +31,7 @@ | |||
31 | 31 | ||
32 | struct mtrand_ { | 32 | struct mtrand_ { |
33 | int mt_index; | 33 | int mt_index; |
34 | usize_t mt_buffer[MT_LEN]; | 34 | uint32_t mt_buffer[MT_LEN]; |
35 | }; | 35 | }; |
36 | 36 | ||
37 | typedef struct mtrand_ mtrand; | 37 | typedef struct mtrand_ mtrand; |
@@ -46,13 +46,13 @@ static void mt_init (mtrand *mt, int seed) { | |||
46 | mt->mt_index = 0; | 46 | mt->mt_index = 0; |
47 | } | 47 | } |
48 | 48 | ||
49 | static usize_t mt_random (mtrand *mt) { | 49 | static uint32_t mt_random (mtrand *mt) { |
50 | usize_t * b = mt->mt_buffer; | 50 | uint32_t * b = mt->mt_buffer; |
51 | int idx = mt->mt_index; | 51 | int idx = mt->mt_index; |
52 | usize_t s; | 52 | uint32_t s; |
53 | int i; | 53 | int i; |
54 | 54 | ||
55 | if (idx == MT_LEN*sizeof(usize_t)) | 55 | if (idx == MT_LEN*sizeof(uint32_t)) |
56 | { | 56 | { |
57 | idx = 0; | 57 | idx = 0; |
58 | i = 0; | 58 | i = 0; |
@@ -68,14 +68,25 @@ static usize_t mt_random (mtrand *mt) { | |||
68 | s = TWIST(b, MT_LEN-1, 0); | 68 | s = TWIST(b, MT_LEN-1, 0); |
69 | b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s); | 69 | b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s); |
70 | } | 70 | } |
71 | mt->mt_index = idx + sizeof(usize_t); | 71 | mt->mt_index = idx + sizeof(uint32_t); |
72 | return *(usize_t *)((unsigned char *)b + idx); | 72 | return *(uint32_t *)((unsigned char *)b + idx); |
73 | } | 73 | } |
74 | 74 | ||
75 | static mtrand static_mtrand; | 75 | static mtrand static_mtrand; |
76 | 76 | ||
77 | #include <math.h> | 77 | #include <math.h> |
78 | 78 | ||
79 | static uint32_t | ||
80 | mt_exp_rand (uint32_t mean, uint32_t max_value) | ||
81 | { | ||
82 | double mean_d = mean; | ||
83 | double erand = log (1.0 / (mt_random (&static_mtrand) / | ||
84 | (double)UINT32_MAX)); | ||
85 | uint32_t x = (uint32_t) (mean_d * erand + 0.5); | ||
86 | |||
87 | return min (x, max_value); | ||
88 | } | ||
89 | |||
79 | #ifndef WIN32 | 90 | #ifndef WIN32 |
80 | #include <sys/wait.h> | 91 | #include <sys/wait.h> |
81 | #endif | 92 | #endif |
@@ -103,8 +114,6 @@ static char TEST_RECON2_FILE[TESTFILESIZE]; | |||
103 | static char TEST_COPY_FILE[TESTFILESIZE]; | 114 | static char TEST_COPY_FILE[TESTFILESIZE]; |
104 | static char TEST_NOPERM_FILE[TESTFILESIZE]; | 115 | static char TEST_NOPERM_FILE[TESTFILESIZE]; |
105 | 116 | ||
106 | static int test_exponential_dist (usize_t mean, usize_t max); | ||
107 | |||
108 | #define CHECK(cond) if (!(cond)) { DP(RINT "check failure: " #cond); abort(); } | 117 | #define CHECK(cond) if (!(cond)) { DP(RINT "check failure: " #cond); abort(); } |
109 | 118 | ||
110 | /* Use a fixed soft config so that test values are fixed. See also | 119 | /* Use a fixed soft config so that test values are fixed. See also |
@@ -149,17 +158,6 @@ static int do_fail (xd3_stream *stream, const char *buf) | |||
149 | return 0; | 158 | return 0; |
150 | } | 159 | } |
151 | 160 | ||
152 | static int | ||
153 | test_exponential_dist (usize_t mean, usize_t max_value) | ||
154 | { | ||
155 | double mean_d = mean; | ||
156 | double erand = log (1.0 / (mt_random (&static_mtrand) / | ||
157 | (double)USIZE_T_MAX)); | ||
158 | usize_t x = (usize_t) (mean_d * erand + 0.5); | ||
159 | |||
160 | return min (x, max_value); | ||
161 | } | ||
162 | |||
163 | /* Test that the exponential distribution actually produces its mean. */ | 161 | /* Test that the exponential distribution actually produces its mean. */ |
164 | static int | 162 | static int |
165 | test_random_numbers (xd3_stream *stream, int ignore) | 163 | test_random_numbers (xd3_stream *stream, int ignore) |
@@ -175,7 +173,7 @@ test_random_numbers (xd3_stream *stream, int ignore) | |||
175 | 173 | ||
176 | for (i = 0; i < n_rounds; i += 1) | 174 | for (i = 0; i < n_rounds; i += 1) |
177 | { | 175 | { |
178 | sum += test_exponential_dist (mean, USIZE_T_MAX); | 176 | sum += mt_exp_rand (mean, USIZE_T_MAX); |
179 | } | 177 | } |
180 | 178 | ||
181 | average = (double) sum / (double) n_rounds; | 179 | average = (double) sum / (double) n_rounds; |
@@ -272,7 +270,7 @@ test_make_inputs (xd3_stream *stream, xoff_t *ss_out, xoff_t *ts_out) | |||
272 | for (i = 0; i < ts; ) | 270 | for (i = 0; i < ts; ) |
273 | { | 271 | { |
274 | size_t left = ts - i; | 272 | size_t left = ts - i; |
275 | size_t next = test_exponential_dist (TEST_ADD_MEAN, TEST_ADD_MAX); | 273 | size_t next = mt_exp_rand (TEST_ADD_MEAN, TEST_ADD_MAX); |
276 | size_t add_left = sadd_max - sadd; | 274 | size_t add_left = sadd_max - sadd; |
277 | double add_prob = (left == 0) ? 0 : (add_left / (double) left); | 275 | double add_prob = (left == 0) ? 0 : (add_left / (double) left); |
278 | int do_copy; | 276 | int do_copy; |
@@ -1120,7 +1118,7 @@ sec_dist_func4 (xd3_stream *stream, xd3_output *data) | |||
1120 | int i, ret, x; | 1118 | int i, ret, x; |
1121 | for (i = 0; i < ALPHABET_SIZE*20; i += 1) | 1119 | for (i = 0; i < ALPHABET_SIZE*20; i += 1) |
1122 | { | 1120 | { |
1123 | x = test_exponential_dist (10, ALPHABET_SIZE/2); | 1121 | x = mt_exp_rand (10, ALPHABET_SIZE/2); |
1124 | if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; } | 1122 | if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; } |
1125 | } | 1123 | } |
1126 | return 0; | 1124 | return 0; |
@@ -1133,7 +1131,7 @@ sec_dist_func5 (xd3_stream *stream, xd3_output *data) | |||
1133 | int i, ret, x; | 1131 | int i, ret, x; |
1134 | for (i = 0; i < ALPHABET_SIZE*20; i += 1) | 1132 | for (i = 0; i < ALPHABET_SIZE*20; i += 1) |
1135 | { | 1133 | { |
1136 | x = test_exponential_dist (10, ALPHABET_SIZE-1); | 1134 | x = mt_exp_rand (10, ALPHABET_SIZE-1); |
1137 | if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; } | 1135 | if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; } |
1138 | } | 1136 | } |
1139 | return 0; | 1137 | return 0; |