t_c_map.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <string.h>
  25. #include <assert.h>
  26. #include <stdio.h>
  27. static int
  28. compare_e ( void *left, void *right ) {
  29. return strcmp ( (const char *)left, (const char *) right );
  30. }
  31. char *char_value[] = { "A","B","C","D","E","F","G","H","I","J","K","L","M",
  32. "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  33. int int_value[] = { 1,2,3,4,5,6,7,8,9,10,
  34. 11,12,13,14,15,16,17,18,19,20,
  35. 21,22,23,24,25,26};
  36. static void
  37. insert_all ( struct clib_map* myMap) {
  38. int size = sizeof(char_value)/sizeof(char_value[0]);
  39. int i = 0;
  40. for ( i = 0; i < size; i++ ) {
  41. char *key = clib_strdup( char_value[i]);
  42. int key_length = (int)strlen ( key ) + 1;
  43. int value = int_value[i];
  44. printf ( "Inserting [%s -> %d]\n", key, value );
  45. insert_clib_map ( myMap, key, key_length, &value, sizeof(int));
  46. free ( key );
  47. }
  48. }
  49. static void
  50. check_exists_all( struct clib_map* myMap) {
  51. int size = sizeof(char_value)/sizeof(char_value[0]);
  52. int i = 0;
  53. for ( i = 0; i < size; i++ ) {
  54. void *value ;
  55. assert ( clib_true == exists_clib_map ( myMap, char_value[i]));
  56. assert ( clib_true == find_clib_map( myMap, char_value[i], &value));
  57. printf ( "-----> [%s == %d]\n", char_value[i], *(int*)value);
  58. assert ( *(int*)value == int_value[i]);
  59. free ( value );
  60. }
  61. }
  62. static void
  63. remove_some_exist(struct clib_map* myMap) {
  64. assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "A"));
  65. assert ( clib_false == exists_clib_map ( myMap, "A"));
  66. assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "X"));
  67. assert ( clib_false == exists_clib_map ( myMap, "X"));
  68. assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "Z"));
  69. assert ( clib_false == exists_clib_map ( myMap, "Z"));
  70. assert ( CLIB_ERROR_SUCCESS == remove_clib_map ( myMap, "H"));
  71. assert ( clib_false == exists_clib_map ( myMap, "H"));
  72. }
  73. static void
  74. add_removed_check_all(struct clib_map* myMap) {
  75. char *key = clib_strdup ("A");
  76. int key_length = (int)strlen ( key ) + 1;
  77. insert_clib_map ( myMap, key, key_length , &int_value[0], sizeof(int));
  78. free ( key );
  79. key = clib_strdup ("X");
  80. key_length = (int)strlen ( key ) + 1;
  81. insert_clib_map ( myMap, key, key_length, &int_value[23], sizeof(int));
  82. free ( key );
  83. key = clib_strdup ("Z");
  84. key_length = (int)strlen ( key ) + 1;
  85. insert_clib_map ( myMap, key, key_length, &int_value[25], sizeof(int));
  86. free ( key );
  87. key = clib_strdup ("H");
  88. key_length = (int)strlen ( key ) + 1;
  89. insert_clib_map ( myMap, key, key_length, &int_value[7 ], sizeof(int));
  90. free ( key );
  91. check_exists_all(myMap);
  92. }
  93. void
  94. test_clib_map() {
  95. struct clib_map* myMap = new_clib_map ( compare_e, NULL, NULL);
  96. insert_all(myMap);
  97. check_exists_all(myMap);
  98. remove_some_exist(myMap);
  99. add_removed_check_all(myMap);
  100. delete_clib_map(myMap);
  101. }