testc_vec.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include "faux/vec.h"
  6. int kmatch(const void *key, const void *item)
  7. {
  8. uint32_t k = *(uint32_t *)key;
  9. uint32_t i = *(uint32_t *)item;
  10. if (k == i)
  11. return 0;
  12. return -1;
  13. }
  14. #define VEC_LEN 6
  15. int testc_faux_vec(void)
  16. {
  17. // Source vector
  18. const uint32_t src_vec[VEC_LEN] = { 0, 1, 2, 3, 4, 5 };
  19. unsigned int i = 0;
  20. int ret = -1; // Pessimistic return value
  21. faux_vec_t *vec = NULL;
  22. // Fill the vector
  23. vec = faux_vec_new(sizeof(uint32_t), kmatch);
  24. for (i = 0; i < VEC_LEN; i++) {
  25. uint32_t *val = faux_vec_add(vec);
  26. *val = src_vec[i];
  27. }
  28. // Compare whole vectors
  29. if (memcmp(faux_vec_data(vec), src_vec,
  30. faux_vec_len(vec) * faux_vec_item_size(vec))) {
  31. fprintf(stderr, "Vector object is not equal to etalon vector\n");
  32. goto err;
  33. }
  34. // Out-of-range
  35. if (faux_vec_item(vec, VEC_LEN) != NULL) {
  36. fprintf(stderr, "Broken out-of-range\n");
  37. goto err;
  38. }
  39. // Get item by index
  40. if (*(uint32_t *)faux_vec_item(vec, VEC_LEN - 2) != (VEC_LEN - 2)) {
  41. fprintf(stderr, "Broken faux_vec_item()\n");
  42. goto err;
  43. }
  44. // Remove last item
  45. if (faux_vec_del(vec, VEC_LEN - 1) != (VEC_LEN - 1)) {
  46. fprintf(stderr, "Broken last item deletion\n");
  47. goto err;
  48. }
  49. if (memcmp(faux_vec_data(vec), src_vec,
  50. faux_vec_len(vec) * faux_vec_item_size(vec))) {
  51. fprintf(stderr, "Vector object is not equal to etalon vector after del\n");
  52. goto err;
  53. }
  54. // Restore full vector
  55. *(uint32_t *)faux_vec_add(vec) = VEC_LEN - 1;
  56. // Remove not-last item
  57. if (faux_vec_del(vec, VEC_LEN - 3) != (VEC_LEN - 1)) {
  58. fprintf(stderr, "Broken non-last item deletion\n");
  59. goto err;
  60. }
  61. // Get item by index but it fill the hole now
  62. if (*(uint32_t *)faux_vec_item(vec, VEC_LEN - 3) != (VEC_LEN - 2)) {
  63. fprintf(stderr, "Broken faux_vec_item() after non-last delete\n");
  64. goto err;
  65. }
  66. // Find item
  67. if (faux_vec_find(vec, &src_vec[VEC_LEN - 1], 0) != (VEC_LEN - 2)) {
  68. fprintf(stderr, "Can't find item by key\n");
  69. goto err;
  70. }
  71. // Find item start from some item
  72. if (faux_vec_find(vec, &src_vec[1], 2) >= 0) {
  73. fprintf(stderr, "The start_index doesn't work\n");
  74. goto err;
  75. }
  76. // Remove all items one by one. Note current len is (VEC_LEN - 1).
  77. // Checks for case when length of vector is 1 and we delete this
  78. // left item.
  79. for (i = 0; i < (VEC_LEN - 1); i++)
  80. faux_vec_del(vec, 0);
  81. ret = 0;
  82. err:
  83. faux_vec_free(vec);
  84. return ret;
  85. }