Skip to content

Commit

Permalink
Kiểm thử ngẫu nhiên rbm & rbs
Browse files Browse the repository at this point in the history
  • Loading branch information
bangoc committed Jan 14, 2022
1 parent 3bc32cc commit e8c6df0
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ add_test(NAME HashSetRandUT COMMAND hset_rand_ut)
add_test(NAME GvecVoidUT COMMAND gvec_v_ut)
add_test(NAME SpacesUT COMMAND spaces_ut)
add_test(NAME SplitUT COMMAND split_ut)
add_test(NAME RbsStrUt COMMAND rbs_str_ut)
add_test(NAME RbsStrUt COMMAND rbs_str_ut)
add_test(NAME RbmRandiUt COMMAND rbm_rand_i_ut 1000)
add_test(NAME RbsRandUt COMMAND rbs_rand_ut 1000)
22 changes: 11 additions & 11 deletions rbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,29 @@ static inline void _rbm_move_next(gtype **k, gtype **v) {
* Tham khảo: #hmap_traverse(key, value, map)
*/
#define rbm_traverse(k, v, map) \
for (gtype *k = &(rbm_node_key(bn_left_most((map)->t.root))), \
*v = &(rbm_node_value(bn_left_most((map)->t.root))); \
for (gtype *k = (rbm_size(map))? &(rbm_node_key(bn_left_most((map)->t.root))): NULL_PTR, \
*v = (rbm_size(map))? &(rbm_node_value(bn_left_most((map)->t.root))): NULL_PTR; \
k != NULL_PTR && v != NULL_PTR; _rbm_move_next(&k, &v)) \

/**
* Giải phóng bộ nhớ được cấp phát cho bảng m. Các hàm free_key và
* free_value được gọi cho từng khóa và giá trị nếu != NULL.
*
* @param t Con trỏ tới bảng cây.
* @param map Con trỏ tới bảng cây.
*/
#define rbm_free(t) \
#define rbm_free(map) \
do { \
if ((t)->free_key || (t)->free_value) { \
rbm_traverse(_k, _v, (t)) { \
if ((t)->free_key) { \
(t)->free_key(*_k); \
if ((map)->free_key || (map)->free_value) { \
rbm_traverse(_k, _v, (map)) { \
if ((map)->free_key) { \
(map)->free_key(*_k); \
} \
if ((t)->free_value) { \
(t)->free_value(*_v); \
if ((map)->free_value) { \
(map)->free_value(*_v); \
} \
} \
} \
bn_free_tree((bn_tree_t)(t)); \
bn_free_tree((bn_tree_t)(map)); \
} while (0)

#endif // RBM_H_
2 changes: 1 addition & 1 deletion rbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static inline void _rbs_move_next(gtype **cur) {
#define rbs_size(s) ((s)->size)

#define rbs_traverse(cur, s) \
for (gtype *cur = &(rbs_node_value(bn_left_most((s)->t.root))); \
for (gtype *cur = (rbs_size(s))? &(rbs_node_value(bn_left_most((s)->t.root))): NULL_PTR; \
cur != NULL_PTR; _rbs_move_next(&cur))

#define rbs_free(s) \
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ add_subdirectory(rbs)
add_subdirectory(p1w)
add_subdirectory(gvec)
add_subdirectory(hset)
add_subdirectory(str)
add_subdirectory(str)
add_subdirectory(rbm)
5 changes: 5 additions & 0 deletions tests/rbm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(rbm_rand_i_ut rbm_rand_i_ut.c)

foreach (p rbm_rand_i_ut)
target_link_libraries(${p} bkc)
endforeach()
61 changes: 61 additions & 0 deletions tests/rbm/rbm_rand_i_ut.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
(C) Nguyen Ba Ngoc 2022
Kiểm tra kịch bản ánh xạ chuỗi -> chuỗi
*/

#include "cgen.h"
#include "tests/base/utils.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define BUFF_SIZE 32
char buff[BUFF_SIZE] = {0};

void gen_buff() {
for (int i = 0; i < BUFF_SIZE - 1; ++i) {
buff[i] = 'a' + rand() % ('z' - 'a' + 1);
buff[BUFF_SIZE - 1];
}
}

int main(int argc, char *argv[]) {
int n;
sscanf(argv[1], "%d", &n);
rbm_t map = rbm_create(gtype_cmp_s, gtype_free_s, gtype_free_s);
srand(time(NULL));
char **keys = malloc(n * sizeof(char *)),
**values = malloc(n * sizeof(char *));
long cc = 0;
for (int i = 0; i < n; ++i) {
gen_buff();
char *tmp = strdup(buff);
rbm_ires ires = rbm_insert(map, gtype_s(tmp), gtype_s(NULL));
if (!ires.inserted) {
CHECK_MSG(rbm_size(map) == bn_size((bn_tree_t)map), "Size equal bn_size");
CHECK_MSG(rbm_size(map) == cc, "size == cc");
free(tmp);
continue;
}
keys[cc] = tmp;
gen_buff();
values[cc] = strdup(buff);
ires.value->s = values[cc];
++cc;
CHECK_MSG(rbm_size(map) == bn_size((bn_tree_t)map), "Size equal bn_size");
CHECK_MSG(rbm_size(map) == cc, "size == cc");
}
int sz = cc;
for (int i = 0; i < sz; ++i) {
CHECK_MSG(rbm_value(map, gtype_s(keys[i]))->s == values[i], "Point to the same place");
CHECK_MSG(rbm_remove(map, gtype_s(keys[i])) == 1, "Remove key i");
--cc;
CHECK_MSG(rbm_size(map) == bn_size((bn_tree_t)map), "Size equal bn_size");
CHECK_MSG(rbm_size(map) == cc, "size == cc");
}
rbm_free(map);
free(keys);
free(values);
TEST_OK();
}
3 changes: 2 additions & 1 deletion tests/rbs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_executable(rbs_i_ut rbs_i_ut.c)
add_executable(rbs_str_ut rbs_str_ut.c)
add_executable(rbs_rand_ut rbs_rand_ut.c)

foreach (p rbs_i_ut rbs_str_ut)
foreach (p rbs_i_ut rbs_str_ut rbs_rand_ut)
target_link_libraries(${p} bkc)
endforeach()
54 changes: 54 additions & 0 deletions tests/rbs/rbs_rand_ut.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
(C) Nguyen Ba Ngoc 2022
Kiểm tra kịch bản ánh xạ chuỗi -> chuỗi
*/

#include "cgen.h"
#include "tests/base/utils.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define BUFF_SIZE 32
char buff[BUFF_SIZE] = {0};

void gen_buff() {
for (int i = 0; i < BUFF_SIZE - 1; ++i) {
buff[i] = 'a' + rand() % ('z' - 'a' + 1);
buff[BUFF_SIZE - 1];
}
}

int main(int argc, char *argv[]) {
int n;
sscanf(argv[1], "%d", &n);
rbs_t s = rbs_create(gtype_cmp_s, gtype_free_s);
srand(time(NULL));
char **keys = malloc(n * sizeof(char *));
long cc = 0;
for (int i = 0; i < n; ++i) {
gen_buff();
char *tmp = strdup(buff);
if (!rbs_insert(s, gtype_s(tmp))) {
CHECK_MSG(rbs_size(s) == bn_size((bn_tree_t)s), "Size equal bn_size");
CHECK_MSG(rbs_size(s) == cc, "size == cc");
free(tmp);
continue;
}
keys[cc] = tmp;
++cc;
CHECK_MSG(rbs_size(s) == bn_size((bn_tree_t)s), "Size equal bn_size");
CHECK_MSG(rbs_size(s) == cc, "size == cc");
}
int sz = cc;
for (int i = 0; i < sz; ++i) {
CHECK_MSG(rbs_remove(s, gtype_s(keys[i])) == 1, "Remove key i");
--cc;
CHECK_MSG(rbs_size(s) == bn_size((bn_tree_t)s), "Size equal bn_size");
CHECK_MSG(rbs_size(s) == cc, "size == cc");
}
rbs_free(s);
free(keys);
TEST_OK();
}

0 comments on commit e8c6df0

Please sign in to comment.