0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <cgreen/cgreen.h>
0039 #include <ubf.h>
0040 #include <ndrstandard.h>
0041 #include <string.h>
0042 #include <ndebug.h>
0043 #include <exbase64.h>
0044 #include <nstdutil.h>
0045 #include <fcntl.h>
0046 #include "test.fd.h"
0047 #include "ubfunit1.h"
0048 #include "xatmi.h"
0049
0050
0051
0052
0053 #define TEST_INDEX(MEM, IDX) ((ndrx_lh_test_t*)(((char*)MEM)+(int)(sizeof(ndrx_lh_test_t)*IDX)))
0054
0055 #define TEST_MEM_MAX 50
0056 #define TEST_RUN_MAX 200
0057
0058
0059
0060
0061
0062
0063 typedef struct ndrx_lh_test ndrx_lh_test_t;
0064 struct ndrx_lh_test
0065 {
0066 int key;
0067 int some_val;
0068 short flags;
0069 };
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 exprivate int lhtest_key_hash(ndrx_lh_config_t *conf, void *key_get, size_t key_len)
0083 {
0084 int *p_key = (int *)key_get;
0085 return *p_key % conf->elmmax;
0086 }
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096 exprivate void lhtest_key_debug(ndrx_lh_config_t *conf, void *key_get,
0097 size_t key_len, char *dbg_out, size_t dbg_len)
0098 {
0099 int *p_key = (int *)key_get;
0100 snprintf(dbg_out, dbg_len, "%d", *p_key);
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110 exprivate void lhtest_val_debug(ndrx_lh_config_t *conf, int idx, char *dbg_out,
0111 size_t dbg_len)
0112 {
0113 int val = TEST_INDEX(*conf->memptr, idx)->some_val;
0114
0115 snprintf(dbg_out, dbg_len, "%d", val);
0116 }
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126 exprivate int lhtest_compare(ndrx_lh_config_t *conf, void *key_get,
0127 size_t key_len, int idx)
0128 {
0129 int *key = (int *)key_get;
0130
0131 if (TEST_INDEX((*conf->memptr), idx)->key==*key)
0132 {
0133 return 0;
0134 }
0135 else
0136 {
0137 return 1;
0138 }
0139 }
0140
0141
0142
0143
0144
0145
0146
0147 exprivate void init_one_free(ndrx_lh_test_t *mem, int mark_free)
0148 {
0149 int i;
0150
0151 memset(mem, 0, sizeof(ndrx_lh_test_t)*TEST_MEM_MAX);
0152
0153 for (i=0; i<TEST_MEM_MAX; i++)
0154 {
0155 if (i!=mark_free)
0156 {
0157 mem[i].some_val=i+5;
0158 mem[i].key=i;
0159 mem[i].flags=NDRX_LH_FLAG_ISUSED|NDRX_LH_FLAG_WASUSED;
0160 }
0161 }
0162 }
0163
0164
0165
0166
0167 Ensure(test_nstd_lh_have_space)
0168 {
0169 int i;
0170 ndrx_lh_test_t test_mem[TEST_MEM_MAX];
0171 void *p = test_mem;
0172 ndrx_lh_config_t conf;
0173
0174 int pos;
0175 int have_value;
0176 int found;
0177
0178 conf.elmmax = TEST_MEM_MAX;
0179 conf.elmsz = sizeof(ndrx_lh_test_t);
0180 conf.flags_offset = EXOFFSET(ndrx_lh_test_t, flags);
0181 conf.memptr = (void **)&p;
0182 conf.p_key_hash=&lhtest_key_hash;
0183 conf.p_key_debug=&lhtest_key_debug;
0184 conf.p_val_debug=&lhtest_val_debug;
0185 conf.p_compare=&lhtest_compare;
0186
0187 for (i=TEST_MEM_MAX; i<TEST_RUN_MAX; i++)
0188 {
0189 init_one_free(test_mem, i % 25);
0190
0191
0192 found=ndrx_lh_position_get(&conf, &i, sizeof(i), O_CREAT, &pos,
0193 &have_value, "lhtest");
0194 assert_equal(found, EXTRUE);
0195
0196
0197 test_mem[pos].key=i;
0198 test_mem[pos].some_val=i+6;
0199 test_mem[pos].flags=NDRX_LH_FLAG_ISUSED|NDRX_LH_FLAG_WASUSED;
0200
0201
0202 found=ndrx_lh_position_get(&conf, &i, sizeof(i), O_CREAT, &pos,
0203 &have_value, "lhtest");
0204
0205 assert_equal(found, EXTRUE);
0206
0207
0208 found=ndrx_lh_position_get(&conf, &i, sizeof(i), 0, &pos,
0209 &have_value, "lhtest");
0210 assert_equal(found, EXTRUE);
0211
0212
0213 assert_equal(test_mem[pos].some_val, i+6);
0214
0215
0216 test_mem[pos].flags=NDRX_LH_FLAG_WASUSED;
0217 found=ndrx_lh_position_get(&conf, &i, sizeof(i), O_CREAT, &pos,
0218 &have_value, "lhtest");
0219 assert_equal(found, EXTRUE);
0220 }
0221 }
0222
0223
0224
0225
0226
0227 TestSuite *ubf_nstd_lh(void)
0228 {
0229 TestSuite *suite = create_test_suite();
0230
0231 add_test(suite, test_nstd_lh_have_space);
0232
0233 return suite;
0234 }
0235