t_c_deque.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. static int
  29. compare_e ( void *left, void *right ) {
  30. int *l = (int*) left;
  31. int *r = (int*) right;
  32. return *l == *r ;
  33. }
  34. static void
  35. free_e ( void *ptr ) {
  36. if ( ptr )
  37. free ( ptr);
  38. }
  39. void
  40. test_clib_deque() {
  41. int flip = 1;
  42. int i = 0;
  43. int limit = 20;
  44. void* element;
  45. int j = 0;
  46. struct clib_deque* myDeq = new_clib_deque ( 10, compare_e, NULL);
  47. assert ( (struct clib_deque*)0 != myDeq );
  48. for ( i = 0; i <= limit; i++ ) {
  49. if ( flip ) {
  50. push_back_clib_deque ( myDeq, &i , sizeof(int));
  51. flip = 0;
  52. } else {
  53. push_front_clib_deque ( myDeq, &i, sizeof(int) );
  54. flip = 1;
  55. }
  56. }
  57. front_clib_deque ( myDeq, &element );
  58. assert ( *(int*)element == limit - 1 );
  59. free ( element );
  60. back_clib_deque ( myDeq, &element );
  61. assert ( *(int*)element == limit);
  62. free ( element );
  63. while ( empty_clib_deque(myDeq) != clib_true ) {
  64. pop_front_clib_deque ( myDeq);
  65. }
  66. delete_clib_deque(myDeq);
  67. myDeq = new_clib_deque ( 10, compare_e, free_e);
  68. for ( i = 0; i <= limit; i ++ ) {
  69. int *v = (int*)malloc(sizeof(int ));
  70. memcpy ( v, &i, sizeof ( int ));
  71. push_back_clib_deque ( myDeq, v , sizeof(int*));
  72. free ( v );
  73. }
  74. for ( i = myDeq->head + 1; i < myDeq->tail; i++ ){
  75. void *elem;
  76. if ( element_at_clib_deque( myDeq, i, &elem ) == CLIB_ERROR_SUCCESS ) {
  77. assert ( *(int*)elem == j++ );
  78. free ( elem );
  79. }
  80. }
  81. delete_clib_deque(myDeq);
  82. }