Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. // testTextbuffer.c
  2. // A stub file for you to test your textbuffer implementation.
  3. // Note that you will NOT be submitting this - this is simply for you to
  4. // test your functions as you go. We will have our own testTexbuffer.c
  5. // for testing your submission.
  6.  
  7. #include <assert.h>
  8. #include <stdbool.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #include "textbuffer.h"
  14.  
  15. static void testNewTB(void);
  16. static void testTexbufferEditing(void);
  17. static void testCombiningTextbuffers(void);
  18. static void testEmptyTextbuffer(void);
  19.  
  20. // TODO: Add more function prototypes
  21.  
  22.  
  23. int main(void) {
  24.  
  25. testNewTB();
  26. testTexbufferEditing();
  27. testCombiningTextbuffers();
  28. testEmptyTextbuffer();
  29.  
  30. // TODO: Call more test functions
  31.  
  32. printf("All tests passed! You are awesome!\n");
  33. }
  34.  
  35. static void testNewTB(void) {
  36.  
  37. printf("-----------------------------------------\n"
  38. " newTB tests \n"
  39. "-----------------------------------------\n");
  40.  
  41. // Calling dumpTB immediately after newTB, without modifying the TB
  42. TB tb1 = newTB("hello there,\n\n\nhow\nare\nthings\n");
  43. char *text1 = dumpTB(tb1, false); // Don't show line numbers
  44. assert(linesTB(tb1) == 6);
  45.  
  46. assert(strcmp("hello there,\n\n\nhow\nare\nthings\n", text1) == 0);
  47. printf("text1 =\"%s\"", text1);
  48. free(text1);
  49.  
  50. releaseTB(tb1);
  51.  
  52. printf("newTB tests passed!\n");
  53.  
  54. }
  55.  
  56. static void testTexbufferEditing(void) {
  57.  
  58. printf("-----------------------------------------\n"
  59. " textbuffer editing tests \n"
  60. "-----------------------------------------\n");
  61.  
  62. // Calling dumpTB immediately after newTB, without modifying the TB
  63. TB tb1 = newTB("room\nmoon\ncow jumping over the moon\nlight\n");
  64. assert(linesTB(tb1) == 4);
  65.  
  66. char *text1 = dumpTB(tb1, false); // Don't show line numbers
  67. printf("\nText before prefix is added: \n%s\n", text1);
  68. free(text1);
  69.  
  70. addPrefixTB(tb1, 1, 3, "goodnight ");
  71.  
  72. char *text2 = dumpTB(tb1, false); // Don't show line numbers
  73. printf("\nText after prefix is added: \n%s\n", text2);
  74. assert(strcmp(text2, "goodnight room\ngoodnight moon\ngoodnight cow jumping over the moon\nlight\n") == 0);
  75. free(text2);
  76.  
  77. char *text3 = dumpTB(tb1, false);
  78. printf("\nText before lines are deleted: \n%s\n", text3);
  79. free(text3);
  80.  
  81. deleteTB(tb1, 1, 3);
  82.  
  83. char *text4 = dumpTB(tb1, false);
  84. printf("\nText after lines are deleted: \n%s\n", text4);
  85. assert(strcmp(text4, "light\n") == 0);
  86. free(text4);
  87.  
  88. printf("\nDeleting last line...\n");
  89. deleteTB(tb1, 1, 1);
  90. assert(linesTB(tb1) == 0);
  91.  
  92. releaseTB(tb1);
  93.  
  94. printf("\ntextbuffer editing tests passed!\n");
  95.  
  96. }
  97.  
  98. static void testCombiningTextbuffers(void) {
  99.  
  100. printf("-----------------------------------------\n"
  101. " combining textbuffer tests \n"
  102. "-----------------------------------------\n");
  103.  
  104. TB tb1 = newTB("Hello\nWhat's\nWrong with you?\n");
  105. TB tb2 = newTB("Bye\nYou're\nbeautiful.. :)\n");
  106.  
  107. mergeTB(tb2, 1, tb1);
  108. char *text1 = dumpTB(tb2, false);
  109. assert(strcmp(text1, "Hello\nWhat's\nWrong with you?\nBye\nYou're\nbeautiful.. :)\n") == 0);
  110. free(text1);
  111.  
  112. TB tb3 = newTB("Peace Out Bruh\nLater!\n");
  113. mergeTB(tb3, 2, tb2);
  114. char *text2 = dumpTB(tb3, false);
  115. assert(strcmp(text2, "Peace Out Bruh\nHello\nWhat's\nWrong with you?\nBye\nYou're\nbeautiful.. :)\nLater!\n") == 0);
  116. free(text2);
  117.  
  118. TB tb4 = newTB("Hello There, I haven't watched Star Wars\n");
  119. mergeTB(tb3, 9, tb4);
  120. char *text3 = dumpTB(tb3, false);
  121. assert(strcmp(text3, "Peace Out Bruh\nHello\nWhat's\nWrong with you?\nBye\nYou're\nbeautiful.. :)\nLater!\nHello There, I haven't watched Star Wars\n") == 0);
  122. free(text3);
  123.  
  124. releaseTB(tb3);
  125. printf("passed all combining textbuffer tests!\n");
  126. }
  127.  
  128. static void testEmptyTextbuffer() {
  129.  
  130. printf("-----------------------------------------\n"
  131. " empty textbuffer tests \n"
  132. "-----------------------------------------\n");
  133.  
  134. TB tb1 = newTB("");
  135. releaseTB(tb1);
  136.  
  137. TB tb2 = newTB("");
  138. assert(linesTB(tb2) == 0);
  139. addPrefixTB(tb2, 1, 3, "yeet");
  140.  
  141. TB tb3 = newTB("bruh\nwhats up\n");
  142. mergeTB(tb3, 1, tb2);
  143.  
  144. mergeTB(tb2, 1, tb3);
  145. char *text1 = dumpTB(tb2, false);
  146. assert(strcmp(text1, "bruh\nwhats up\n") == 0);
  147. free(text1);
  148.  
  149. releaseTB(tb2);
  150.  
  151. printf("passed all empty textbuffer tests!\n");
  152.  
  153. }
  154.  
  155.  
  156.  
  157. // TODO: Add more test functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement