Skip to content

Commit

Permalink
Generate random integer array for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hutusi committed Apr 3, 2020
1 parent 893820e commit 6d182b3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ make test
- [x] Red Black Tree [rbtree.h](src/rbtree.h) [rbtree.c](src/rbtree.c)
- [x] Binary Heap [heap.h](src/heap.h) [heap.c](src/heap.c)
- [ ] Fibonacci Heap, Binomial Heap
- [ ] Skip List
- [x] Skip List [skip_list.h](src/skip_list.h) [skip_list.c](src/skip_list.c)
- [ ] B+ Tree

### Graphs
Expand Down
6 changes: 6 additions & 0 deletions src/skip_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*
*/

/**
* The implementaion of skip list reference to:
* https://www.geeksforgeeks.org/skip-list/
*
*/

#include "skip_list.h"
#include "def.h"
#include <assert.h>
Expand Down
2 changes: 0 additions & 2 deletions src/skip_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*
* @brief Skip List.
*
* ref: https://www.geeksforgeeks.org/skip-list/
*
* @date 2020-03-29
*
* @copyright Copyright (c) 2020, hutusi.com
Expand Down
20 changes: 20 additions & 0 deletions test/test_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@

#include "alloc-testing.h"

#define SWAP(a, b, tmp) tmp=a; a=b; b=tmp;

int string_equal(void *str1, void *str2)
{
return strcmp((char *)str1, (char *)str2) == 0;
}

int *generate_random_numbers(int from, int to)
{
int count = to - from + 1;
int *arr = (int *)malloc(sizeof(int) * count);

for (int i = 0; i < count; i++) {
arr[i] = i + from;
}

int tmp;
for (int i = 0; i < count; i++) {
int j = rand()%count;
SWAP(arr[i], arr[j], tmp);
}

return arr;
}
2 changes: 2 additions & 0 deletions test/test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ static inline void assert_double_eq(double a, double b, double tolerance)

int string_equal(void *str1, void *str2);

int *generate_random_numbers(int from, int to);

#endif /* RETHINK_C_TEST_HELPER_H */
15 changes: 6 additions & 9 deletions test/test_skip_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,26 @@
void test_skip_list_insert()
{
SkipList *list = skip_list_new(int_compare, NULL, NULL);
int arr[] = {5, 6, 3, 2, 8, 1, 4, 0, 9, 7, 13, 11, 14,
18, 19, 17, 16, 12, 15, 10, 23, 21, 24, 28, 29, 27,
26, 22, 25, 20, 33, 31, 34, 38, 39, 37, 36, 32, 35,
30, 43, 41, 44, 48, 49, 47, 46, 42, 45, 40};
int *arr = generate_random_numbers(0, 99);

// assert(skip_list_insert(list, &(arr[0]), &(arr[0])));
for (int i = 0; i < 50; ++i) {
for (int i = 0; i < 100; ++i) {
int *key = &(arr[i]);
int *value = key;
assert(skip_list_insert(list, key, value));
}

skip_list_print(list);

int k = 20;
assert(k == *((int *)skip_list_find(list, &k)));

SkipListNode *node = skip_list_remove_node(list, &(arr[9]));
skip_list_free_node(list, node);

skip_list_print(list);

int k = 20;
assert(k == *((int *)skip_list_find(list, &k)));

skip_list_free(list);
free(arr);
}

void test_skip_list()
Expand Down

0 comments on commit 6d182b3

Please sign in to comment.