Skip to content

Commit ce3472a

Browse files
Added unit tests for random number generator functions.
1 parent ae333ba commit ce3472a

File tree

7 files changed

+275
-35
lines changed

7 files changed

+275
-35
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CC = gcc
22

3-
OBJS = lib_test.o lib_ll.o lib_sort.o lib_vbtree.o
3+
OBJS = lib_test.o lib_hash.o lib_ll.o lib_random.o lib_sort.o lib_vbtree.o
44
PROG = dstruct
55
INSTALLDIR = /usr/local/bin
66

lib_hash.c

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <assert.h>
2+
#include <limits.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#include "lib_hash.h"
7+
8+
int is_prime(int num)
9+
{
10+
int i;
11+
assert(num >= 0);
12+
if(num == 1 || num == 0) return 0;
13+
for(i = 2 ; i <= num / 2; i++)
14+
if(num % i == 0) return 0;
15+
return 1;
16+
}
17+
18+
int next_twinprime(int num)
19+
{
20+
assert(num >= 2);
21+
while(num < INT_MAX) {
22+
num++;
23+
if(is_prime(num) && is_prime(num-2))return num;
24+
}
25+
return -1;
26+
}
27+
28+
Hash hash_new(int len)
29+
{
30+
Hash h;
31+
assert(len >= 2);
32+
h.size = len;
33+
h.data = malloc(sizeof(int)*len);
34+
return h;
35+
}
36+
37+
Hash hash_new_prime(int len)
38+
{
39+
Hash h;
40+
assert(len >= 2);
41+
len = next_twinprime(len);
42+
h.size = len;
43+
h.data = malloc(sizeof(int)*len);
44+
return h;
45+
}
46+
47+
int hash_insert(Hash h, void * data, size_t length)
48+
{
49+
assert(data != NULL);
50+
51+
return 0;
52+
}
53+
54+
void * hash_search(Hash h, void * data, size_t length)
55+
{
56+
return NULL;
57+
}
58+
59+
int hash_remove(Hash h, void * data, size_t length)
60+
{
61+
assert(data != NULL);
62+
return 0;
63+
}
64+
65+
int hash_empty(Hash h)
66+
{
67+
return 0;
68+
}
69+
70+
int hash_full(Hash h)
71+
{
72+
return 0;
73+
}
74+
75+
void hash_clear(Hash h)
76+
{
77+
78+
}
79+
80+
void hash_print(Hash h)
81+
{
82+
83+
}
84+
85+
void hash_delete(Hash h)
86+
{
87+
free(&h);
88+
}
89+
90+
91+
/*
92+
93+
int main()
94+
{
95+
int j, loc, distinct, key, num[N + 1];
96+
FILE * in = fopen("numbers.in", "r");
97+
for(j = 1; j <= N; j++)
98+
num[j] = Empty;
99+
distinct = 0;
100+
while(fscanf(in, "%d", &key) == 1) {
101+
loc = loc % N + 1;
102+
if(num[loc] == Empty) {
103+
if(distinct == MaxNumbers) {
104+
printf("\nTable Full: %d not added\n", key);
105+
exit(1);
106+
}
107+
num[loc] = key;
108+
distinct++;
109+
}
110+
}
111+
printf("\nThere are %d distinct numbers\n", distinct);
112+
fclose(in);
113+
}
114+
*/

lib_hash.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef LIB_HASH_H_
2+
#define LIB_HASH_H_
3+
4+
#define EMPTY 0
5+
6+
typedef struct hash {
7+
int * data;
8+
int size;
9+
} Hash;
10+
11+
/*
12+
create new hash with len length
13+
returns null pointer when hash could not be created.
14+
*/
15+
Hash hash_new(int len);
16+
17+
/*
18+
same as hash_new accept:
19+
tests input length - determine if number is double prime.
20+
If not, finds next larger double prime for size of hash.
21+
*/
22+
Hash hash_new_prime(int len);
23+
24+
/* insert data into hash, must be sent with size of data */
25+
int hash_insert(Hash h, void * data, size_t length);
26+
27+
/* search hash for data value, return address of data */
28+
void * hash_search(Hash h, void * data, size_t length);
29+
30+
/* remove data from hash, must be sent with size of data removed */
31+
int hash_remove(Hash h, void * data, size_t length);
32+
33+
/* test for empty hash - no data stored */
34+
int hash_empty(Hash h);
35+
36+
/* test for hash that is completely full */
37+
int hash_full(Hash h);
38+
39+
/* remove all data from hash */
40+
void hash_clear(Hash h);
41+
42+
/* print hash contents to stdout - used for debugging */
43+
void hash_print(Hash h);
44+
45+
/* delete hash and all its contents */
46+
void hash_delete(Hash h);
47+
48+
/* find next double prime number - used for lenth of arrays */
49+
int is_prime(int num);
50+
51+
/* find next prime number where number-2 is also prime */
52+
int next_twinprime(int num);
53+
54+
#endif /* LIB_HASH_H_ */

lib_ll.h

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#ifndef LIB_LL_H_
22
#define LIB_LL_H_
33

4+
typedef struct list_node {
5+
void *pData;
6+
struct list_node *pNext;
7+
} List_Node;
8+
9+
typedef struct {
10+
struct list_node *pNext;
11+
int count;
12+
} List_Head;
13+
414
/* return address of next node in list */
515
#define list_next(element) ((element)->pNext)
616

@@ -22,18 +32,6 @@
2232
/* return integer value of the size of the list */
2333
#define list_size(list) ((list)->count)
2434

25-
typedef struct list_node {
26-
void *pData;
27-
struct list_node *pNext;
28-
} List_Node;
29-
30-
31-
32-
typedef struct {
33-
struct list_node *pNext;
34-
int count;
35-
} List_Head;
36-
3735
/* create new empty list */
3836
List_Head *list_new(void);
3937

lib_random.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <assert.h>
2+
#include <limits.h>
3+
#include <stdlib.h>
4+
#include <time.h>
5+
6+
#include "lib_random.h"
7+
8+
int random_int(int min, int max)
9+
{
10+
assert(min > INT_MIN);
11+
assert(max < INT_MAX / 2);
12+
assert(max >= min);
13+
return rand() % max + min;
14+
}
15+
16+
void random_seed()
17+
{
18+
srand(time(0));
19+
}

lib_random.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef LIB_RANDOM_H_
2+
#define LIB_RANDOM_H_
3+
4+
/* generate random number from min to max */
5+
int random_int(int min, int max);
6+
7+
/* use clock in system to create random seed for number generator */
8+
void random_seed();
9+
10+
#endif /* LIB_RANDOM_H_ */

0 commit comments

Comments
 (0)