Mercurial > hg > xemacs-beta
comparison src/tests.c @ 489:4a8bb4aa9740
[xemacs-hg @ 2001-04-30 08:49:24 by martinb]
hash table mapping
author | martinb |
---|---|
date | Mon, 30 Apr 2001 08:49:26 +0000 |
parents | 74fd4e045ea6 |
children | b39c14581166 |
comparison
equal
deleted
inserted
replaced
488:1e7b510d04f6 | 489:4a8bb4aa9740 |
---|---|
27 | 27 |
28 #include <config.h> | 28 #include <config.h> |
29 #include "lisp.h" | 29 #include "lisp.h" |
30 #include "buffer.h" | 30 #include "buffer.h" |
31 #include "lstream.h" | 31 #include "lstream.h" |
32 #include "elhash.h" | |
32 #include "opaque.h" | 33 #include "opaque.h" |
33 | 34 |
34 static Lisp_Object Vtest_function_list; | 35 static Lisp_Object Vtest_function_list; |
35 | 36 |
36 | 37 |
407 | 408 |
408 return intern ("PASS"); | 409 return intern ("PASS"); |
409 } | 410 } |
410 | 411 |
411 | 412 |
413 /* Hash Table testing */ | |
414 | |
415 typedef struct | |
416 { | |
417 Lisp_Object hash_table; | |
418 EMACS_INT sum; | |
419 } test_hash_tables_data; | |
420 | |
421 | |
422 static int | |
423 test_hash_tables_mapper (Lisp_Object key, Lisp_Object value, | |
424 void *extra_arg) | |
425 { | |
426 test_hash_tables_data *p = (test_hash_tables_data *) extra_arg; | |
427 p->sum += XINT (value); | |
428 return 0; | |
429 } | |
430 | |
431 static int | |
432 test_hash_tables_modifying_mapper (Lisp_Object key, Lisp_Object value, | |
433 void *extra_arg) | |
434 { | |
435 test_hash_tables_data *p = (test_hash_tables_data *) extra_arg; | |
436 Fputhash (make_int (- XINT (key)), | |
437 make_int (2 * XINT (value)), | |
438 p->hash_table); | |
439 p->sum += XINT (value); | |
440 return 0; | |
441 } | |
442 | |
443 static int | |
444 test_hash_tables_predicate (Lisp_Object key, Lisp_Object value, | |
445 void *extra_arg) | |
446 { | |
447 return XINT (key) < 0; | |
448 } | |
449 | |
450 | |
451 DEFUN ("test-hash-tables", Ftest_hash_tables, 0, 0, "", /* | |
452 Test C interface to hash tables. | |
453 */ | |
454 ()) | |
455 { | |
456 test_hash_tables_data data; | |
457 data.hash_table = make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, | |
458 HASH_TABLE_EQUAL); | |
459 | |
460 Fputhash (make_int (1), make_int (2), data.hash_table); | |
461 Fputhash (make_int (3), make_int (4), data.hash_table); | |
462 | |
463 data.sum = 0; | |
464 elisp_maphash_unsafe (test_hash_tables_mapper, | |
465 data.hash_table, (void *) &data); | |
466 assert (data.sum == 2 + 4); | |
467 | |
468 data.sum = 0; | |
469 elisp_maphash (test_hash_tables_modifying_mapper, | |
470 data.hash_table, (void *) &data); | |
471 assert (data.sum == 2 + 4); | |
472 | |
473 /* hash table now contains: (1, 2) (3, 4) (-1, 2*2) (-3, 2*4) */ | |
474 | |
475 data.sum = 0; | |
476 elisp_maphash_unsafe (test_hash_tables_mapper, | |
477 data.hash_table, (void *) &data); | |
478 assert (data.sum == 3 * (2 + 4)); | |
479 | |
480 /* Remove entries with negative keys, added by modifying mapper */ | |
481 elisp_map_remhash (test_hash_tables_predicate, | |
482 data.hash_table, 0); | |
483 | |
484 data.sum = 0; | |
485 elisp_maphash_unsafe (test_hash_tables_mapper, | |
486 data.hash_table, (void *) &data); | |
487 assert (data.sum == 2 + 4); | |
488 | |
489 return intern ("PASS"); | |
490 } | |
491 | |
492 | |
412 | 493 |
413 #define TESTS_DEFSUBR(Fname) do { \ | 494 #define TESTS_DEFSUBR(Fname) do { \ |
414 DEFSUBR (Fname); \ | 495 DEFSUBR (Fname); \ |
415 Vtest_function_list = \ | 496 Vtest_function_list = \ |
416 Fcons (intern (subr_name (&S##Fname)), \ | 497 Fcons (intern (subr_name (&S##Fname)), \ |
421 syms_of_tests (void) | 502 syms_of_tests (void) |
422 { | 503 { |
423 Vtest_function_list = Qnil; | 504 Vtest_function_list = Qnil; |
424 | 505 |
425 TESTS_DEFSUBR (Ftest_data_format_conversion); | 506 TESTS_DEFSUBR (Ftest_data_format_conversion); |
507 TESTS_DEFSUBR (Ftest_hash_tables); | |
426 /* Add other test functions here with TESTS_DEFSUBR */ | 508 /* Add other test functions here with TESTS_DEFSUBR */ |
427 } | 509 } |
428 | 510 |
429 void | 511 void |
430 vars_of_tests (void) | 512 vars_of_tests (void) |