Skip to content

Commit 01447d6

Browse files
committed
File operations finished
1 parent c2d12c6 commit 01447d6

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

src/article.c

+17-5
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,28 @@ bool articles_are_equal(const Article_t* const a, const Article_t* const b)
8181

8282
void display_article(const Article_t* article, FILE* out)
8383
{
84-
fprintf(out, "O email de %s eh %s\n", article->doi, article->title);
84+
fprintf(out,
85+
"\tDOI: %s\n"
86+
"\tTitle: %s\n"
87+
"\tAuthor: %s\n"
88+
"\tYear: %u\n",
89+
article->doi, article->title, article->author, article->year);
90+
}
91+
92+
void read_line_to(FILE* in, char* const target)
93+
{
94+
fscanf(in, "%[^\n]s", target);
95+
fgetc(in);
8596
}
8697

8798
Article_t* article_from_file(FILE* in)
8899
{
89-
Article_t * empty_article = make_article("", "", "", 0);
100+
Article_t* empty_article = make_article("", "", "", 0);
101+
102+
read_line_to(in, empty_article->doi);
103+
read_line_to(in, empty_article->title);
104+
read_line_to(in, empty_article->author);
90105

91-
fscanf(in, "%s\n", empty_article->doi);
92-
fscanf(in, "%s\n", empty_article->title);
93-
fscanf(in, "%s\n", empty_article->author);
94106
fscanf(in, "%u\n", &empty_article->year);
95107

96108
return empty_article;

src/main.c

+31-17
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ void test_hash_table_file_operations_empty_table()
218218
HashTable_t* ht = ht_new();
219219

220220
// Dumping empty hash table
221-
FILE* fp = fopen("hash.bin", "wb");
221+
FILE* fp = fopen("hash.bin", "w");
222222
ht_dump(ht, fp);
223223

224224
ht_delete(ht);
225225

226226
// Reading ht from same file
227-
freopen("hash.bin", "rb+", fp);
227+
freopen("hash.bin", "r", fp);
228228
ht = ht_from_file(fp);
229229

230230
// Make sure it's an empty hash table
@@ -240,12 +240,12 @@ void test_hash_table_file_operations_resized_table()
240240
{// Can dump/read resized ht
241241
HashTable_t* ht = ht_new();
242242
unsigned long resized_capacity = ht_capacity(ht) + 1;
243-
FILE* fp = fopen("hash.bin", "wb");
243+
FILE* fp = fopen("hash.bin", "w");
244244

245245
ht_resize(ht, resized_capacity);
246246
ht_dump(ht, fp);
247247

248-
freopen("hash.bin", "rb", fp);
248+
freopen("hash.bin", "r", fp);
249249
ht_delete(ht);
250250
ht = ht_from_file(fp);
251251

@@ -261,12 +261,12 @@ void test_hash_table_file_operations_one_article()
261261
HashTable_t* ht = ht_new();
262262
const char* const a_key = "DOI";
263263
Article_t* const a = make_article(a_key, "Title", "Author", 2000);
264-
FILE* fp = fopen("hash.bin", "wb");
264+
FILE* fp = fopen("hash.bin", "w");
265265

266266
ht_insert(ht, a);
267267
ht_dump(ht, fp);
268268

269-
freopen("hash.bin", "rb+", fp);
269+
freopen("hash.bin", "r", fp);
270270
ht_delete(ht);
271271
ht = ht_from_file(fp);
272272

@@ -282,37 +282,51 @@ void test_hash_table_file_operations_one_article()
282282
ht_delete(ht);
283283
}
284284

285-
void test_hash_table_file_operations()
285+
void test_hash_table_file_operations_two_articles()
286286
{
287-
test_hash_table_file_operations_empty_table();
288-
test_hash_table_file_operations_resized_table();
289-
test_hash_table_file_operations_one_article();
290-
291287
HashTable_t* ht = ht_new();
292-
const char* const a_key = "DOI";
293-
const char* const b_key = "Other_DOI";
294-
Article_t* const a = make_article(a_key, "Title", "Author", 2000);
295-
Article_t* const b = make_article(b_key, "Title", "Author", 2000);
296-
FILE* fp = fopen("hash.bin", "wb");
288+
const char* const a_key = "DOI_without_spaces";
289+
const char* const b_key = "DOI with spaces";
290+
Article_t* const a = make_article(a_key, "Title with spaces", "Author", 2000);
291+
Article_t* const b = make_article(b_key, "Title_without_spaces", "Author with spaces", 2000);
292+
FILE* fp = fopen("hash.bin", "w");
297293

298294
ht_insert(ht, a);
299295
ht_insert(ht, b);
300296
ht_dump(ht, fp);
301297
ht_delete(ht);
302298

303-
freopen("hash.bin", "rb", fp);
299+
freopen("hash.bin", "r", fp);
304300
ht = ht_from_file(fp);
305301

306302
assert(ht_is_empty(ht) == false);
307303
assert(ht_count(ht) == 2);
308304

305+
assert(ht_contains(ht, a_key) == true);
306+
const Article_t* fetched = ht_fetch(ht, a_key);
307+
assert(fetched != NULL);
308+
assert(articles_are_equal(a, fetched));
309+
310+
assert(ht_contains(ht, b_key) == true);
311+
fetched = ht_fetch(ht, b_key);
312+
assert(fetched != NULL);
313+
assert(articles_are_equal(b, fetched));
314+
309315
debug("Can dump ht with two articles");
310316

311317
delete_article(a);
312318
delete_article(b);
313319
ht_delete(ht);
314320
}
315321

322+
void test_hash_table_file_operations()
323+
{
324+
test_hash_table_file_operations_empty_table();
325+
test_hash_table_file_operations_resized_table();
326+
test_hash_table_file_operations_one_article();
327+
test_hash_table_file_operations_two_articles();
328+
}
329+
316330
void print_test_status()
317331
{
318332
if (global_failure)

0 commit comments

Comments
 (0)