t-varbuf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * t-verbuf.c - test varbuf implementation
  4. *
  5. * Copyright © 2009 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <dpkg-test.h>
  22. #include <dpkg-db.h>
  23. static void
  24. test_varbuf_init(void)
  25. {
  26. struct varbuf vb;
  27. varbufinit(&vb, 0);
  28. test_pass(vb.used == 0);
  29. test_pass(vb.size == 0);
  30. test_pass(vb.buf == NULL);
  31. varbuffree(&vb);
  32. test_pass(vb.used == 0);
  33. test_pass(vb.size == 0);
  34. test_pass(vb.buf == NULL);
  35. }
  36. static void
  37. test_varbuf_prealloc(void)
  38. {
  39. struct varbuf vb;
  40. varbufinit(&vb, 10);
  41. test_pass(vb.used == 0);
  42. test_pass(vb.size >= 10);
  43. test_pass(vb.buf != NULL);
  44. varbuffree(&vb);
  45. test_pass(vb.used == 0);
  46. test_pass(vb.size == 0);
  47. test_pass(vb.buf == NULL);
  48. }
  49. static void
  50. test_varbuf_addbuf(void)
  51. {
  52. struct varbuf vb;
  53. varbufinit(&vb, 5);
  54. varbufaddbuf(&vb, "1234567890", 10);
  55. test_pass(vb.used == 10);
  56. test_pass(vb.size >= vb.used);
  57. test_mem(vb.buf, ==, "1234567890", 10);
  58. varbufaddbuf(&vb, "abcde", 5);
  59. test_pass(vb.used == 15);
  60. test_pass(vb.size >= vb.used);
  61. test_mem(vb.buf, ==, "1234567890abcde", 15);
  62. varbuffree(&vb);
  63. }
  64. static void
  65. test_varbuf_addc(void)
  66. {
  67. struct varbuf vb;
  68. varbufinit(&vb, 1);
  69. varbufaddc(&vb, 'a');
  70. test_pass(vb.used == 1);
  71. test_pass(vb.size >= vb.used);
  72. test_pass(vb.buf[0] == 'a');
  73. varbufaddc(&vb, 'b');
  74. test_pass(vb.used == 2);
  75. test_pass(vb.size >= vb.used);
  76. test_mem(vb.buf, ==, "ab", 2);
  77. varbufaddc(&vb, 'c');
  78. test_pass(vb.used == 3);
  79. test_pass(vb.size >= vb.used);
  80. test_mem(vb.buf, ==, "abc", 3);
  81. varbufaddc(&vb, 'd');
  82. test_pass(vb.used == 4);
  83. test_pass(vb.size >= vb.used);
  84. test_mem(vb.buf, ==, "abcd", 4);
  85. varbuffree(&vb);
  86. }
  87. static void
  88. test_varbuf_dupc(void)
  89. {
  90. struct varbuf vb;
  91. varbufinit(&vb, 5);
  92. varbufdupc(&vb, 'z', 10);
  93. test_pass(vb.used == 10);
  94. test_pass(vb.size >= vb.used);
  95. test_mem(vb.buf, ==, "zzzzzzzzzz", 10);
  96. varbufdupc(&vb, 'y', 5);
  97. test_pass(vb.used == 15);
  98. test_pass(vb.size >= vb.used);
  99. test_mem(vb.buf, ==, "zzzzzzzzzzyyyyy", 15);
  100. varbuffree(&vb);
  101. }
  102. static void
  103. test_varbuf_substc(void)
  104. {
  105. struct varbuf vb;
  106. varbufinit(&vb, 5);
  107. varbufaddbuf(&vb, "1234a5678a9012a", 15);
  108. varbufsubstc(&vb, 'a', 'z');
  109. test_pass(vb.used == 15);
  110. test_pass(vb.size >= vb.used);
  111. test_mem(vb.buf, ==, "1234z5678z9012z", 15);
  112. varbuffree(&vb);
  113. }
  114. static void
  115. test_varbuf_reset(void)
  116. {
  117. struct varbuf vb;
  118. varbufinit(&vb, 10);
  119. varbufaddbuf(&vb, "1234567890", 10);
  120. varbufreset(&vb);
  121. test_pass(vb.used == 0);
  122. test_pass(vb.size >= vb.used);
  123. varbufaddbuf(&vb, "abcdefghijklmno", 15);
  124. test_pass(vb.used == 15);
  125. test_pass(vb.size >= vb.used);
  126. test_mem(vb.buf, ==, "abcdefghijklmno", 15);
  127. varbuffree(&vb);
  128. }
  129. static void
  130. test(void)
  131. {
  132. test_varbuf_init();
  133. test_varbuf_prealloc();
  134. test_varbuf_addbuf();
  135. test_varbuf_addc();
  136. test_varbuf_dupc();
  137. test_varbuf_substc();
  138. test_varbuf_reset();
  139. /* FIXME: Complete. */
  140. }