t_c_set.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  2. * This file is part of clib library
  3. * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  23. #include "c_lib.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <assert.h>
  28. static void
  29. delete_e ( void *ptr ) {
  30. if ( ptr )
  31. free ( ptr );
  32. }
  33. static int
  34. compare_e ( void *left, void *right ) {
  35. char *l = (char*)left;
  36. char *r = (char*)right;
  37. return strcmp ( (const char *)l, (const char *) r );
  38. }
  39. static int
  40. compare_int ( void *left, void *right ) {
  41. int *l = (int*)left;
  42. int *r = (int*)right;
  43. if ( *l < *r )
  44. return -1;
  45. else if ( *l > *r )
  46. return 1;
  47. return 0;
  48. }
  49. void
  50. test_clib_set(){
  51. {
  52. int test[] = { 0,1,2,3,4,5,6,7,8,9 };
  53. int index = 0;
  54. int size = sizeof (test) /sizeof(test[0]);
  55. void* outKey;
  56. struct clib_set *pSet = new_clib_set ( compare_int, NULL);
  57. for ( index = 0; index < size; index++ ) {
  58. int v = test[index];
  59. insert_clib_set ( pSet, &v, sizeof(int));
  60. }
  61. for ( index = 0; index < size; index++ ) {
  62. int v = test[index];
  63. assert ( clib_true == exists_clib_set ( pSet, &v));
  64. }
  65. index = 9;
  66. find_clib_set ( pSet, &index, &outKey);
  67. assert ( 9 == *(int*)outKey);
  68. free ( outKey );
  69. delete_clib_set(pSet);
  70. }
  71. {
  72. typedef struct test {
  73. char *string;
  74. } TEST_INPUT;
  75. int index = 0;
  76. int size = 0;
  77. char *v;
  78. TEST_INPUT ti[] ={
  79. {"A for APPLE"},{"B for BALL"},{"C for CAT"}, {"D for DOG"},
  80. {"E for ELEPHANT"},{"F for FISH"},{"G for GOAT"},
  81. {"H for HORSE"},{"I for ICECREAM"},{"J for JOKER"},
  82. {"K for KITE"},{"L for LAMB"},{"M for MONKEY"},
  83. {"N for NEST"},{"O for ORANGE"},{"P for POT"},
  84. {"Q for QUEEN"},{"R for RAT"},{"S for SHEEP"},
  85. {"T for TABLE"},{"U for UMBRELLA"},{"V for VIOLIN"},{"W for WAX"},
  86. {"X for XEROX"},{"Y for YUMMY"},{"Z for ZEBRA"}
  87. };
  88. struct clib_set *pSet = new_clib_set ( compare_e, delete_e);
  89. size = sizeof ( ti ) / sizeof ( ti[0]);
  90. for ( index = 0; index < size; index++ ){
  91. char *temp = clib_strdup ( ti[index].string );
  92. insert_clib_set ( pSet, temp, strlen(temp) + 1 );
  93. free ( temp );
  94. }
  95. for ( index = 0; index < size; index++ ){
  96. v = ti[index].string;
  97. assert ( clib_true == exists_clib_set ( pSet, v));
  98. }
  99. delete_clib_set(pSet);
  100. }
  101. }