-
Notifications
You must be signed in to change notification settings - Fork 0
/
happly.c
121 lines (100 loc) · 3.38 KB
/
happly.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* happly.c -- tests happly() function in hash.c
*
*/
#include "hash.h"
#include "queue-objects.h"
#include "car-functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define HASHSIZE 20
#define MAXREG 10
void func0(car_ft *cp) {
char plate[MAXREG];
double price;
int year;
strcpy(plate, "US-8000");
price = 10000;
year = 2016;
change_all(cp, plate, price, year);
}
int32_t main() {
personq_t *person1, *person2, *person3, *result_person;
carq_t *car1, *car2, *car3, *result_car;
int32_t result_int;
hashtable_t *htp;
char name_person1[] = "Devanshi", name_person2[] = "Robert", name_person3[] = "Edmund";
char plate_car1[] = "US-1000", plate_car2[] = "US-2000", plate_car3[] = "US-3000";
double height_person1 = 6.5, height_person2 = 6.4, height_person3 = 6.1;
double price_car1 = 36000, price_car2 = 38000, price_car3 = 40000;
int age_person1 = 29, age_person2 = 20 , age_person3 = 19;
int year_car1 = 2018, year_car2 = 2019 , year_car3 = 2020;
/*
person1 = make_car(name_person1, age_person1, height_person1);
person2 = make_car(name_person2, age_person2, height_person2);
person3 = make_car(name_person3, age_person3, height_person3);
car1 = make_car(plate_car1, price_car1, year_car1);
car2 = make_car(plate_car2, price_car2, year_car2);
car3 = make_car(plate_car3, price_car3, year_car3);
*/
/*
* Apply function to a non-existing hashtable
*/
happly(htp, func0);
/*
* Open a new empty hashtable
*/
htp = hopen(HASHSIZE);
/*
* Now, apply function to the existing but empty hashtable
*/
happly(htp, func0);
/*
* Hash some cars into the hashtable
*/
hput(htp, car1, car1->plate, sizeof(car1->plate));
hput(htp, car2, car2->plate, sizeof(car2->plate));
hput(htp, car3, car3->plate, sizeof(car3->plate));
/*
* Then, apply function to the existing and non-empty hashtable
*/
happly(htp, func0);
/*
* Check to see if function application worked on car1 using hsearch() function
*/
result_car = hsearch(htp, compare_plate, plate_car1, sizeof(plate_car1));
if(result_car) {
fprintf(stderr, "Test Error: Apply function did not work! 'hsearch()' should not be able to find car with the given plate from hashtable\n");
free(car1);
free(car2);
free(car3);
exit(EXIT_FAILURE);
}
/*
* Check to see if function application worked on car2 using hsearch() function
*/
result_car = hsearch(htp, compare_plate, plate_car2, sizeof(plate_car2));
if(result_car) {
fprintf(stderr, "Test Error: Apply function did not work! 'hsearch()' should not be able to find car with the given plate from hashtable\n");
free(car1);
free(car2);
free(car3);
exit(EXIT_FAILURE);
}
/*
* Check to see if function application worked on car3 using hsearch() function
*/
result_car = hsearch(htp, compare_plate, plate_car3, sizeof(plate_car3));
if(result_car) {
fprintf(stderr, "Test Error: Apply function did not work! 'hsearch()' should not be able to find car with the given plate from hashtable\n");
free(car1);
free(car2);
free(car3);
exit(EXIT_FAILURE);
}
free(car1);
free(car2);
free(car3);
exit(EXIT_SUCCESS);
}