Skip to content

Commit f8f3d7c

Browse files
committed
Make-shift file operations
1 parent 63e633d commit f8f3d7c

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

include/article.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66

77
typedef struct Article_s Article_t;
88

9+
// Constructors/Destructors
910
Article_t* make_article(const char* doi, const char* title, const char* author, unsigned int year);
1011
Article_t* duplicate_article(const Article_t* original);
12+
Article_t* article_from_file(FILE* in);
1113
void delete_article(Article_t* a);
1214

15+
// Queries
1316
const char* key_of(const Article_t* article);
1417
bool article_has_key(const Article_t* article, const char* key);
1518
bool articles_are_equal(const Article_t* a, const Article_t* b);
1619

20+
// Commands
1721
void display_article(const Article_t* article, FILE* out);
22+
void dump_article(const Article_t* article, FILE* out);
1823

1924
#endif //ARTICLE_H

include/hashtable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct HashTable_s HashTable_t;
1010

1111
// Constructors/Destructors
1212
HashTable_t* ht_new(void);
13+
HashTable_t* ht_from_file(FILE* in);
1314
void ht_delete(HashTable_t* ht);
1415

1516
// Queries
@@ -26,5 +27,6 @@ void ht_resize(HashTable_t* ht, unsigned long new_capacity);
2627
void ht_expand(HashTable_t* ht);
2728
void ht_shrink(HashTable_t* ht);
2829
void ht_display_states(HashTable_t* ht, FILE* out);
30+
void ht_dump(const HashTable_t* ht, FILE* out);
2931

3032
#endif //HASH_TABLE_H

src/article.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,13 @@ void display_article(const Article_t* article, FILE* out)
8383
{
8484
fprintf(out, "O email de %s eh %s\n", article->doi, article->title);
8585
}
86+
87+
Article_t* article_from_file(FILE* in)
88+
{
89+
return make_article("DOI", "Title", "Author", 2000);
90+
}
91+
92+
void dump_article(const Article_t* article, FILE* out)
93+
{
94+
fprintf(out, "a");
95+
}

src/hashtable.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,41 @@ void ht_display_states(HashTable_t* ht, FILE* out)
316316
" 01234567890123456789012345678901234567890123456789\n"
317317
"-------------------------------------------------------------\n");
318318
}
319+
320+
ht_index_t read_capacity(FILE* in)
321+
{
322+
ht_index_t capacity;
323+
fscanf(in, "%lu", &capacity);
324+
return capacity;
325+
}
326+
327+
HashTable_t* ht_from_file(FILE* const in)
328+
{
329+
HashTable_t* ht = ht_new();
330+
331+
ht_resize(ht, read_capacity(in));
332+
333+
if (!feof(in))
334+
{
335+
puts("Hey there");
336+
Article_t * a = article_from_file(in);
337+
ht_insert(ht, a);
338+
delete_article(a);
339+
}
340+
341+
return ht;
342+
}
343+
344+
void ht_dump(const HashTable_t* ht, FILE* const out)
345+
{
346+
fprintf(out, "%lu", ht_capacity(ht));
347+
348+
for (ht_index_t i = 0; i < ht_capacity(ht); ++i)
349+
{
350+
if (ht->states[i] == OCCUPIED)
351+
{
352+
dump_article(ht->items[i], out);
353+
break;
354+
}
355+
}
356+
}

src/main.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,78 @@ void test_hash_table_resize()
213213
ht_delete(ht);
214214
}
215215

216+
void test_hash_table_file_operations_empty_table()
217+
{
218+
HashTable_t* ht = ht_new();
219+
220+
// Dumping empty hash table
221+
FILE* fp = fopen("hash.bin", "wb");
222+
ht_dump(ht, fp);
223+
224+
ht_delete(ht);
225+
226+
// Reading ht from same file
227+
freopen("hash.bin", "r", fp);
228+
ht = ht_from_file(fp);
229+
230+
// Make sure it's an empty hash table
231+
assert(ht_is_empty(ht) == true);
232+
assert(ht_count(ht) == 0);
233+
debug("Can dump empty table");
234+
235+
ht_delete(ht);
236+
fclose(fp);
237+
}
238+
239+
void test_hash_table_file_operations_resized_table()
240+
{// Can dump/read resized ht
241+
HashTable_t* ht = ht_new();
242+
unsigned long resized_capacity = ht_capacity(ht) + 1;
243+
FILE* fp = fopen("hash.bin", "wb");
244+
245+
ht_resize(ht, resized_capacity);
246+
ht_dump(ht, fp);
247+
248+
freopen("hash.bin", "r", fp);
249+
ht_delete(ht);
250+
ht = ht_from_file(fp);
251+
252+
assert(ht_capacity(ht) == resized_capacity);
253+
debug("Can dump resized table");
254+
255+
ht_delete(ht);
256+
fclose(fp);
257+
}
258+
259+
void test_hash_table_file_operations()
260+
{
261+
test_hash_table_file_operations_empty_table();
262+
test_hash_table_file_operations_resized_table();
263+
264+
// Test hashtable with one item
265+
HashTable_t* ht = ht_new();
266+
const char* const a_key = "DOI";
267+
Article_t* const a = make_article(a_key, "Title", "Author", 2000);
268+
FILE* fp = fopen("hash.bin", "wb");
269+
270+
ht_insert(ht, a);
271+
ht_dump(ht, fp);
272+
273+
freopen("hash.bin", "r", fp);
274+
ht_delete(ht);
275+
ht = ht_from_file(fp);
276+
277+
assert(ht_is_empty(ht) == false);
278+
assert(ht_count(ht) == 1);
279+
assert(ht_contains(ht, a_key) == true);
280+
const Article_t* const fetched = ht_fetch(ht, a_key);
281+
assert(fetched != NULL);
282+
assert(articles_are_equal(a, fetched));
283+
284+
delete_article(a);
285+
ht_delete(ht);
286+
}
287+
216288
void print_test_status()
217289
{
218290
if (global_failure)
@@ -238,6 +310,7 @@ int main(void)
238310
test_hash_table_multiple_articles();
239311
test_hash_table_insert_override_key();
240312
test_hash_table_resize();
313+
test_hash_table_file_operations();
241314

242315
global_failure = false;
243316

0 commit comments

Comments
 (0)