Skip to content

Commit c2d12c6

Browse files
committed
I failed the binary challenge
1 parent f8f3d7c commit c2d12c6

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

src/article.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ void display_article(const Article_t* article, FILE* out)
8686

8787
Article_t* article_from_file(FILE* in)
8888
{
89-
return make_article("DOI", "Title", "Author", 2000);
89+
Article_t * empty_article = make_article("", "", "", 0);
90+
91+
fscanf(in, "%s\n", empty_article->doi);
92+
fscanf(in, "%s\n", empty_article->title);
93+
fscanf(in, "%s\n", empty_article->author);
94+
fscanf(in, "%u\n", &empty_article->year);
95+
96+
return empty_article;
9097
}
9198

9299
void dump_article(const Article_t* article, FILE* out)
93100
{
94-
fprintf(out, "a");
101+
fprintf(out, "%s\n", article->doi);
102+
fprintf(out, "%s\n", article->title);
103+
fprintf(out, "%s\n", article->author);
104+
fprintf(out, "%u\n", article->year);
95105
}

src/hashtable.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void ht_display_states(HashTable_t* ht, FILE* out)
320320
ht_index_t read_capacity(FILE* in)
321321
{
322322
ht_index_t capacity;
323-
fscanf(in, "%lu", &capacity);
323+
fscanf(in, "%lu\n", &capacity);
324324
return capacity;
325325
}
326326

@@ -330,10 +330,10 @@ HashTable_t* ht_from_file(FILE* const in)
330330

331331
ht_resize(ht, read_capacity(in));
332332

333-
if (!feof(in))
333+
while (!feof(in))
334334
{
335335
puts("Hey there");
336-
Article_t * a = article_from_file(in);
336+
Article_t* a = article_from_file(in);
337337
ht_insert(ht, a);
338338
delete_article(a);
339339
}
@@ -343,14 +343,13 @@ HashTable_t* ht_from_file(FILE* const in)
343343

344344
void ht_dump(const HashTable_t* ht, FILE* const out)
345345
{
346-
fprintf(out, "%lu", ht_capacity(ht));
346+
fprintf(out, "%lu\n", ht->capacity);
347347

348348
for (ht_index_t i = 0; i < ht_capacity(ht); ++i)
349349
{
350350
if (ht->states[i] == OCCUPIED)
351351
{
352352
dump_article(ht->items[i], out);
353-
break;
354353
}
355354
}
356355
}

src/main.c

+37-9
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void test_hash_table_file_operations_empty_table()
224224
ht_delete(ht);
225225

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

230230
// Make sure it's an empty hash table
@@ -245,7 +245,7 @@ void test_hash_table_file_operations_resized_table()
245245
ht_resize(ht, resized_capacity);
246246
ht_dump(ht, fp);
247247

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

@@ -256,12 +256,8 @@ void test_hash_table_file_operations_resized_table()
256256
fclose(fp);
257257
}
258258

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
259+
void test_hash_table_file_operations_one_article()
260+
{// Test hashtable with one item
265261
HashTable_t* ht = ht_new();
266262
const char* const a_key = "DOI";
267263
Article_t* const a = make_article(a_key, "Title", "Author", 2000);
@@ -270,7 +266,7 @@ void test_hash_table_file_operations()
270266
ht_insert(ht, a);
271267
ht_dump(ht, fp);
272268

273-
freopen("hash.bin", "r", fp);
269+
freopen("hash.bin", "rb+", fp);
274270
ht_delete(ht);
275271
ht = ht_from_file(fp);
276272

@@ -280,8 +276,40 @@ void test_hash_table_file_operations()
280276
const Article_t* const fetched = ht_fetch(ht, a_key);
281277
assert(fetched != NULL);
282278
assert(articles_are_equal(a, fetched));
279+
debug("Can dump ht with one article");
280+
281+
delete_article(a);
282+
ht_delete(ht);
283+
}
284+
285+
void test_hash_table_file_operations()
286+
{
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+
291+
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");
297+
298+
ht_insert(ht, a);
299+
ht_insert(ht, b);
300+
ht_dump(ht, fp);
301+
ht_delete(ht);
302+
303+
freopen("hash.bin", "rb", fp);
304+
ht = ht_from_file(fp);
305+
306+
assert(ht_is_empty(ht) == false);
307+
assert(ht_count(ht) == 2);
308+
309+
debug("Can dump ht with two articles");
283310

284311
delete_article(a);
312+
delete_article(b);
285313
ht_delete(ht);
286314
}
287315

0 commit comments

Comments
 (0)