Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // testTextbuffer.c
- // A stub file for you to test your textbuffer implementation.
- // Note that you will NOT be submitting this - this is simply for you to
- // test your functions as you go. We will have our own testTexbuffer.c
- // for testing your submission.
- #include <assert.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "textbuffer.h"
- static void testNewTB(void);
- static void testTexbufferEditing(void);
- static void testCombiningTextbuffers(void);
- static void testEmptyTextbuffer(void);
- // TODO: Add more function prototypes
- int main(void) {
- testNewTB();
- testTexbufferEditing();
- testCombiningTextbuffers();
- testEmptyTextbuffer();
- // TODO: Call more test functions
- printf("All tests passed! You are awesome!\n");
- }
- static void testNewTB(void) {
- printf("-----------------------------------------\n"
- " newTB tests \n"
- "-----------------------------------------\n");
- // Calling dumpTB immediately after newTB, without modifying the TB
- TB tb1 = newTB("hello there,\n\n\nhow\nare\nthings\n");
- char *text1 = dumpTB(tb1, false); // Don't show line numbers
- assert(linesTB(tb1) == 6);
- assert(strcmp("hello there,\n\n\nhow\nare\nthings\n", text1) == 0);
- printf("text1 =\"%s\"", text1);
- free(text1);
- releaseTB(tb1);
- printf("newTB tests passed!\n");
- }
- static void testTexbufferEditing(void) {
- printf("-----------------------------------------\n"
- " textbuffer editing tests \n"
- "-----------------------------------------\n");
- // Calling dumpTB immediately after newTB, without modifying the TB
- TB tb1 = newTB("room\nmoon\ncow jumping over the moon\nlight\n");
- assert(linesTB(tb1) == 4);
- char *text1 = dumpTB(tb1, false); // Don't show line numbers
- printf("\nText before prefix is added: \n%s\n", text1);
- free(text1);
- addPrefixTB(tb1, 1, 3, "goodnight ");
- char *text2 = dumpTB(tb1, false); // Don't show line numbers
- printf("\nText after prefix is added: \n%s\n", text2);
- assert(strcmp(text2, "goodnight room\ngoodnight moon\ngoodnight cow jumping over the moon\nlight\n") == 0);
- free(text2);
- char *text3 = dumpTB(tb1, false);
- printf("\nText before lines are deleted: \n%s\n", text3);
- free(text3);
- deleteTB(tb1, 1, 3);
- char *text4 = dumpTB(tb1, false);
- printf("\nText after lines are deleted: \n%s\n", text4);
- assert(strcmp(text4, "light\n") == 0);
- free(text4);
- printf("\nDeleting last line...\n");
- deleteTB(tb1, 1, 1);
- assert(linesTB(tb1) == 0);
- releaseTB(tb1);
- printf("\ntextbuffer editing tests passed!\n");
- }
- static void testCombiningTextbuffers(void) {
- printf("-----------------------------------------\n"
- " combining textbuffer tests \n"
- "-----------------------------------------\n");
- TB tb1 = newTB("Hello\nWhat's\nWrong with you?\n");
- TB tb2 = newTB("Bye\nYou're\nbeautiful.. :)\n");
- mergeTB(tb2, 1, tb1);
- char *text1 = dumpTB(tb2, false);
- assert(strcmp(text1, "Hello\nWhat's\nWrong with you?\nBye\nYou're\nbeautiful.. :)\n") == 0);
- free(text1);
- TB tb3 = newTB("Peace Out Bruh\nLater!\n");
- mergeTB(tb3, 2, tb2);
- char *text2 = dumpTB(tb3, false);
- assert(strcmp(text2, "Peace Out Bruh\nHello\nWhat's\nWrong with you?\nBye\nYou're\nbeautiful.. :)\nLater!\n") == 0);
- free(text2);
- TB tb4 = newTB("Hello There, I haven't watched Star Wars\n");
- mergeTB(tb3, 9, tb4);
- char *text3 = dumpTB(tb3, false);
- 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);
- free(text3);
- releaseTB(tb3);
- printf("passed all combining textbuffer tests!\n");
- }
- static void testEmptyTextbuffer() {
- printf("-----------------------------------------\n"
- " empty textbuffer tests \n"
- "-----------------------------------------\n");
- TB tb1 = newTB("");
- releaseTB(tb1);
- TB tb2 = newTB("");
- assert(linesTB(tb2) == 0);
- addPrefixTB(tb2, 1, 3, "yeet");
- TB tb3 = newTB("bruh\nwhats up\n");
- mergeTB(tb3, 1, tb2);
- mergeTB(tb2, 1, tb3);
- char *text1 = dumpTB(tb2, false);
- assert(strcmp(text1, "bruh\nwhats up\n") == 0);
- free(text1);
- releaseTB(tb2);
- printf("passed all empty textbuffer tests!\n");
- }
- // TODO: Add more test functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement