| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "config.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <math.h> |
| 16 |
#include <unistd.h> |
| 17 |
|
| 18 |
#include "parse_table.h" |
| 19 |
#include "table.h" |
| 20 |
|
| 21 |
struct test_definition { |
| 22 |
int pass; |
| 23 |
int size; |
| 24 |
int multiple; |
| 25 |
}; |
| 26 |
|
| 27 |
static struct test_definition tests[5] = { |
| 28 |
{ .pass = 1, .size = 0 }, |
| 29 |
{ .pass = 2, .size = 10000 }, |
| 30 |
{ .pass = 3, .size = 1000000 }, |
| 31 |
{ .pass = 4, .size = 0, .multiple = 16 }, |
| 32 |
{ .pass = -1 } |
| 33 |
}; |
| 34 |
|
| 35 |
struct test_value { |
| 36 |
int value; |
| 37 |
char *expected; |
| 38 |
}; |
| 39 |
|
| 40 |
static struct test_value values[9] = { |
| 41 |
{ .value = 0x0020, .expected = "space" }, |
| 42 |
{ .value = 0x00A3, .expected = "_pound_" }, |
| 43 |
{ .value = 0x014B, .expected = "ng" }, |
| 44 |
{ .value = 0x0BAD, .expected = NULL }, |
| 45 |
{ .value = 0x4000, .expected = NULL }, |
| 46 |
{ .value = 0xC0DE, .expected = "code" }, |
| 47 |
{ .value = 0xCAFE, .expected = "cafe" }, |
| 48 |
{ .value = 0xFACE, .expected = "face" }, |
| 49 |
{ .value = 0 } |
| 50 |
}; |
| 51 |
|
| 52 |
static char *table_filename = "fixtures/test_table_resize.tbl"; |
| 53 |
|
| 54 |
#define HEADER_LEN 256 |
| 55 |
#define MESSAGE_LEN 1024 |
| 56 |
|
| 57 |
#test test_table_resize |
| 58 |
table_t *table; |
| 59 |
table_t *new_table; |
| 60 |
char *check; |
| 61 |
char *header; |
| 62 |
char *message; |
| 63 |
int i, j; |
| 64 |
int size; |
| 65 |
int use_hash; |
| 66 |
|
| 67 |
header = malloc(HEADER_LEN); |
| 68 |
message = malloc(MESSAGE_LEN); |
| 69 |
|
| 70 |
for (use_hash = 0; use_hash <= 1; use_hash++) { |
| 71 |
table = parse_table(table_filename); |
| 72 |
|
| 73 |
for (i = 0; tests[i].pass != -1; i++) { |
| 74 |
snprintf(header, HEADER_LEN, "pass = %d, use_hash = %d", tests[i].pass, use_hash); |
| 75 |
#ifdef DEBUG |
| 76 |
printf("%s\n", header); |
| 77 |
#endif |
| 78 |
|
| 79 |
if (tests[i].size > 0) { |
| 80 |
#ifdef DEBUG |
| 81 |
printf("resizing table to size %d\n", tests[i].size); |
| 82 |
#endif |
| 83 |
new_table = table_resize(table, tests[i].size, use_hash); |
| 84 |
table_free(table); |
| 85 |
table = new_table; |
| 86 |
new_table = NULL; |
| 87 |
} else if (tests[i].multiple > 0) { |
| 88 |
size = ((int) ceil(table->used / tests[i].multiple) + 1) * tests[i].multiple; |
| 89 |
#ifdef DEBUG |
| 90 |
printf("resizing table to size %d (as a multiple of %d)\n", size, tests[i].multiple); |
| 91 |
#endif |
| 92 |
new_table = table_resize(table, size, use_hash); |
| 93 |
table_free(table); |
| 94 |
table = new_table; |
| 95 |
new_table = NULL; |
| 96 |
} |
| 97 |
|
| 98 |
#ifdef DEBUG |
| 99 |
printf("table length: %d\ntable used: %d\n", table->length, table->used); |
| 100 |
#endif |
| 101 |
|
| 102 |
for (j = 0; values[j].value != 0; j++) { |
| 103 |
check = table_get(table, values[j].value); |
| 104 |
#ifdef DEBUG |
| 105 |
printf("\t0x%04x -> \"%s\"\n", values[j].value, check); |
| 106 |
#endif |
| 107 |
snprintf( |
| 108 |
message, |
| 109 |
MESSAGE_LEN, |
| 110 |
"%s, value = 0x%04X, expected = %s, output = %s", |
| 111 |
header, |
| 112 |
values[j].value, |
| 113 |
values[j].expected, |
| 114 |
check |
| 115 |
); |
| 116 |
|
| 117 |
if (values[j].expected != NULL) { |
| 118 |
ck_assert_msg(check != NULL, "%s", message); |
| 119 |
ck_assert_msg(strcmp(check, values[j].expected) == 0, "%s", message); |
| 120 |
} else { |
| 121 |
ck_assert_msg(check == NULL, "%s", message); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
table_free(table); |
| 127 |
} |