@@ -224,7 +224,7 @@ void test_hash_table_file_operations_empty_table()
224
224
ht_delete (ht );
225
225
226
226
// Reading ht from same file
227
- freopen ("hash.bin" , "r " , fp );
227
+ freopen ("hash.bin" , "rb+ " , fp );
228
228
ht = ht_from_file (fp );
229
229
230
230
// Make sure it's an empty hash table
@@ -245,7 +245,7 @@ void test_hash_table_file_operations_resized_table()
245
245
ht_resize (ht , resized_capacity );
246
246
ht_dump (ht , fp );
247
247
248
- freopen ("hash.bin" , "r " , fp );
248
+ freopen ("hash.bin" , "rb " , fp );
249
249
ht_delete (ht );
250
250
ht = ht_from_file (fp );
251
251
@@ -256,12 +256,8 @@ void test_hash_table_file_operations_resized_table()
256
256
fclose (fp );
257
257
}
258
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
259
+ void test_hash_table_file_operations_one_article ()
260
+ {// Test hashtable with one item
265
261
HashTable_t * ht = ht_new ();
266
262
const char * const a_key = "DOI" ;
267
263
Article_t * const a = make_article (a_key , "Title" , "Author" , 2000 );
@@ -270,7 +266,7 @@ void test_hash_table_file_operations()
270
266
ht_insert (ht , a );
271
267
ht_dump (ht , fp );
272
268
273
- freopen ("hash.bin" , "r " , fp );
269
+ freopen ("hash.bin" , "rb+ " , fp );
274
270
ht_delete (ht );
275
271
ht = ht_from_file (fp );
276
272
@@ -280,8 +276,40 @@ void test_hash_table_file_operations()
280
276
const Article_t * const fetched = ht_fetch (ht , a_key );
281
277
assert (fetched != NULL );
282
278
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" );
283
310
284
311
delete_article (a );
312
+ delete_article (b );
285
313
ht_delete (ht );
286
314
}
287
315
0 commit comments