t_c_slist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. free_element ( void *ptr ) {
  30. if ( ptr )
  31. free ( ptr);
  32. }
  33. void
  34. add_elements_to_list( struct clib_slist* ll, int x, int y ) {
  35. int i = 0;
  36. for ( i = x; i <= y; i++ ) {
  37. int *v = ( int *) malloc ( sizeof ( int ));
  38. memcpy ( v, &i, sizeof ( int ));
  39. push_back_clib_slist ( ll, v , sizeof(v));
  40. free ( v );
  41. }
  42. }
  43. void
  44. print_e ( void *ptr ) {
  45. if ( ptr )
  46. printf ( "%d\n", *(int*)ptr);
  47. }
  48. static int
  49. compare_element ( void *left, void *right ) {
  50. int *l = (int*) left;
  51. int *r = (int*) right;
  52. return *l == *r ;
  53. }
  54. void
  55. test_clib_slist() {
  56. int i = 0;
  57. int *v;
  58. void* outValue;
  59. struct clib_slist* list = new_clib_slist(free_element,compare_element);
  60. add_elements_to_list(list,1, 10 );
  61. for_each_clib_slist(list, print_e);
  62. i = 55;
  63. v = ( int *) malloc ( sizeof ( int ));
  64. memcpy ( v, &i, sizeof ( int ));
  65. insert_clib_slist(list,5, v,sizeof(v));
  66. free ( v );
  67. for_each_clib_slist(list, print_e);
  68. remove_clib_slist(list,5);
  69. for_each_clib_slist(list, print_e);
  70. remove_clib_slist(list,0);
  71. for_each_clib_slist(list, print_e);
  72. remove_clib_slist(list,100);
  73. for_each_clib_slist(list, print_e);
  74. i = 1;
  75. v = ( int *) malloc ( sizeof ( int ));
  76. memcpy ( v, &i, sizeof ( int ));
  77. insert_clib_slist(list,1,v,sizeof(v));
  78. free ( v );
  79. for_each_clib_slist(list, print_e);
  80. i = 11;
  81. v = ( int *) malloc ( sizeof ( int ));
  82. memcpy ( v, &i, sizeof ( int ));
  83. insert_clib_slist(list,11,v,sizeof(v));
  84. free ( v );
  85. for_each_clib_slist(list, print_e);
  86. i = 12;
  87. v = ( int *) malloc ( sizeof ( int ));
  88. memcpy ( v, &i, sizeof ( int ));
  89. insert_clib_slist(list,200,v,sizeof(v));
  90. free ( v );
  91. for_each_clib_slist(list, print_e);
  92. remove_clib_slist(list,list->size);
  93. for_each_clib_slist(list, print_e);
  94. i = 10;
  95. if ( clib_true == find_clib_slist ( list, &i, &outValue)) {
  96. assert ( i == *(int*)outValue );
  97. free ( outValue );
  98. }
  99. i = 100;
  100. assert ( clib_false == find_clib_slist ( list, &i, &outValue));
  101. delete_clib_slist ( list );
  102. }