Last active
January 2, 2016 19:09
-
-
Save geekuillaume/8348778 to your computer and use it in GitHub Desktop.
Test main for ex00 of day03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include "String.h" | |
#define KGRN "\x1B[32m" | |
#define KRED "\x1B[31m" | |
#define KRES "\033[0m" | |
void testCondition(char *fct_name, char *res, int test) | |
{ | |
if (!test) | |
printf("[%s] %s : %s %s%s\n", fct_name, res, KRED, "BAD", KRES); | |
else | |
printf("[%s] %s : %s %s%s\n", fct_name, res, KGRN, "GOOD", KRES); | |
} | |
int main() | |
{ | |
String test1; | |
String test2; | |
char tmp_char[100]; | |
int tmp; | |
size_t tmp_size; | |
char const *tmp_char_ptr; | |
char **ptr_ptr_char; | |
String *ptr_string; | |
StringDestroy(&test1); | |
testCondition("StringDestroy", "String Destroyed before init", 1); | |
StringInit(&test1, "Test1 Text"); | |
testCondition("StringInit", "String Initialized", test1.str != NULL); | |
StringDestroy(&test1); | |
testCondition("StringDestroy", "String Destroyed", test1.str == NULL); | |
StringDestroy(&test1); | |
testCondition("StringDestroy", "Double Destroy", test1.str == NULL); | |
printf("\n---- ASSIGN ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
test1.assign_c(&test1, "coucou"); | |
testCondition("assign_c", "String assigned", strcmp("coucou", test1.str) == 0); | |
StringDestroy(&test1); | |
StringInit(&test1, "Test1 Text"); | |
StringInit(&test2, "Test2 Text"); | |
test1.assign_s(&test1, &test2); | |
testCondition("assign_s", "String assigned", strcmp("Test2 Text", test1.str) == 0); | |
StringDestroy(&test1); | |
StringDestroy(&test2); | |
printf("\n---- APPEND ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
test1.append_c(&test1, "coucou"); | |
testCondition("append_c", "String appended", strcmp("Test1 Textcoucou", test1.str) == 0); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
test1.append_c(&test1, "coucou"); | |
StringDestroy(&test1); | |
StringInit(&test1, "Test1 Text"); | |
StringInit(&test2, "Test2 Text"); | |
test1.append_s(&test1, &test2); | |
testCondition("append_s", "String appended", strcmp("Test1 TextTest2 Text", test1.str) == 0); | |
StringDestroy(&test1); | |
StringDestroy(&test2); | |
printf("\n---- AT ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
testCondition("at", "First Char", test1.at(&test1, 0) == 'T'); | |
testCondition("at", "Middle Char", test1.at(&test1, 7) == 'e'); | |
testCondition("at", "Invalid negative position", test1.at(&test1, -5) == -1); | |
testCondition("at", "Invalid over position", test1.at(&test1, 5000) == -1); | |
StringDestroy(&test1); | |
printf("\n---- CLEAR ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
test1.clear(&test1); | |
testCondition("clear", "Clear text", strlen(test1.str) == 0); | |
StringDestroy(&test1); | |
printf("\n---- SIZE ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
tmp = test1.size(&test1); | |
testCondition("size", "Test basic size", tmp == 10); | |
test1.clear(&test1); | |
tmp = test1.size(&test1); | |
testCondition("size", "Test empty size", tmp == 0); | |
StringDestroy(&test1); | |
tmp = test1.size(&test1); | |
testCondition("size", "Test destroyed string", tmp == -1); | |
printf("\n---- COMPARE ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
StringInit(&test2, "Test2 Text"); | |
tmp = test1.compare_s(&test1, &test2); | |
testCondition("compare_s", "Compare two basics strings", tmp == strcmp("Test1 Text", "Test2 Text")); | |
StringDestroy(&test1); | |
StringDestroy(&test2); | |
StringInit(&test1, "Test1 Text"); | |
tmp = test1.compare_c(&test1, "coucou"); | |
testCondition("compare_c", "Compare a String with a char *", tmp == strcmp(strdup("Test1 Text"), strdup("coucou"))); | |
StringDestroy(&test1); | |
printf("\n---- COPY ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
tmp_size = test1.copy(&test1, tmp_char, 3, 6); | |
testCondition("copy", "Length returned is good one", tmp_size == 3); | |
tmp_char[tmp_size] = '\0'; | |
testCondition("copy", "String copied is ok", strcmp(tmp_char, "Tex") == 0); | |
tmp_size = test1.copy(&test1, tmp_char, 3, 50); | |
testCondition("copy", "Length returned with incorrect position is 0", tmp_size == 0); | |
tmp_size = test1.copy(&test1, tmp_char, 50, 3); | |
testCondition("copy", "Length returned with too big length is correct", tmp_size == 7); | |
StringDestroy(&test1); | |
tmp_size = test1.copy(&test1, tmp_char, 50, 3); | |
testCondition("copy", "With a destroyed string", tmp_size == 0); | |
printf("\n---- C_STR ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
tmp_char_ptr = test1.c_str(&test1); | |
testCondition("c_str", "Buffer returned is good", strcmp(tmp_char_ptr, "Test1 Text") == 0); | |
StringDestroy(&test1); | |
printf("\n---- EMPTY ----\n"); | |
StringInit(&test1, "Test1 Text"); | |
tmp = test1.empty(&test1); | |
testCondition("empty", "Test while String is not empty", tmp == -1); | |
test1.clear(&test1); | |
tmp = test1.empty(&test1); | |
testCondition("empty", "Test while String is empty", tmp == 1); | |
StringDestroy(&test1); | |
tmp = test1.empty(&test1); | |
testCondition("empty", "Test on destroyed string", tmp == 1); | |
printf("\n---- FIND ----\n"); | |
StringInit(&test1, "coucou salut"); | |
tmp = test1.find_c(&test1, " sa", 0); | |
testCondition("find_c", "Basic test", tmp == 6); | |
tmp = test1.find_c(&test1, "cou", 2); | |
testCondition("find_c", "Test with position", tmp == 3); | |
tmp = test1.find_c(&test1, "cou", 50); | |
testCondition("find_c", "Test with bigger than size position", tmp == -1); | |
tmp = test1.find_c(&test1, "im not here", 0); | |
testCondition("find_c", "Test with unfound string", tmp == -1); | |
StringDestroy(&test1); | |
StringInit(&test1, "coucou salut"); | |
StringInit(&test2, " sa"); | |
tmp = test1.find_s(&test1, &test2, 0); | |
testCondition("find_s", "Basic test", tmp == 6); | |
test2.assign_c(&test2, "cou"); | |
tmp = test1.find_s(&test1, &test2, 2); | |
testCondition("find_s", "Test with position", tmp == 3); | |
tmp = test1.find_s(&test1, &test2, 50); | |
testCondition("find_s", "Test with bigger than size position", tmp == -1); | |
test2.assign_c(&test2, "im not here"); | |
tmp = test1.find_s(&test1, &test2, 0); | |
testCondition("find_s", "Test with unfound string", tmp == -1); | |
StringDestroy(&test1); | |
StringDestroy(&test2); | |
printf("\n---- INSERT ----\n"); | |
StringInit(&test1, "coucou salut"); | |
test1.insert_c(&test1, 2, "inserted"); | |
testCondition("insert_c", "Test basic string", strcmp(test1.str, "coinserteducou salut") == 0); | |
test1.assign_c(&test1, "coucou salut"); | |
test1.insert_c(&test1, 50, "inserted"); | |
testCondition("insert_c", "Test basic string with too big position", strcmp(test1.str, "coucou salutinserted") == 0); | |
StringDestroy(&test1); | |
StringInit(&test1, "coucou salut"); | |
StringInit(&test2, "inserted"); | |
test1.insert_s(&test1, 2, &test2); | |
testCondition("insert_s", "Test basic string", strcmp(test1.str, "coinserteducou salut") == 0); | |
test1.assign_c(&test1, "coucou salut"); | |
test1.insert_s(&test1, 50, &test2); | |
testCondition("insert_s", "Test basic string with too big position", strcmp(test1.str, "coucou salutinserted") == 0); | |
StringDestroy(&test1); | |
StringDestroy(&test2); | |
printf("\n---- TO_INT ----\n"); | |
StringInit(&test1, "42"); | |
tmp = test1.to_int(&test1); | |
testCondition("to_int", "Test with basic number", tmp == 42); | |
StringDestroy(&test1); | |
printf("\n---- SPLIT ----\n"); | |
StringInit(&test1, "salut comment ca"); | |
ptr_ptr_char = test1.split_c(&test1, ' '); | |
testCondition("split_c", "Test basic string", strcmp(ptr_ptr_char[0], "salut") == 0 && strcmp(ptr_ptr_char[1], "comment") == 0 && strcmp(ptr_ptr_char[2], "ca") == 0); | |
StringDestroy(&test1); | |
StringInit(&test1, "salut comment ca"); | |
ptr_string = test1.split_s(&test1, ' '); | |
testCondition("split_s", "Test basic string", strcmp(ptr_string[0].str, "salut") == 0 && strcmp(ptr_string[1].str, "comment") == 0 && strcmp(ptr_string[2].str, "ca") == 0); | |
StringDestroy(&test1); | |
printf("\n---- AFF ----\n"); | |
StringInit(&test1, "test42"); | |
testCondition("aff", "Should show 'test42' with four dash before and after", 1); | |
write(1, "----", 4); | |
test1.aff(&test1); | |
write(1, "----\n", 5); | |
StringDestroy(&test1); | |
printf("\n---- JOIN ----\n"); | |
StringInit(&test1, "test42"); | |
ptr_ptr_char = malloc(4 * sizeof(*ptr_ptr_char)); | |
ptr_ptr_char[0] = strdup("coucou"); | |
ptr_ptr_char[1] = strdup("salut"); | |
ptr_ptr_char[2] = strdup("hello"); | |
ptr_ptr_char[3] = NULL; | |
test1.join_c(&test1, ',', (const char **)ptr_ptr_char); | |
testCondition("join_c", "Test with basic string", strcmp(test1.str, "coucou,salut,hello") == 0); | |
StringDestroy(&test1); | |
StringInit(&test1, "test42"); | |
ptr_string = malloc(4 * sizeof(*ptr_string)); | |
StringInit(&ptr_string[0], "coucou"); | |
StringInit(&ptr_string[1], "salut"); | |
StringInit(&ptr_string[2], "hello"); | |
StringInit(&ptr_string[3], ""); | |
test1.join_s(&test1, ',', ptr_string); | |
testCondition("join_s", "Test with basic string", strcmp(test1.str, "coucou,salut,hello") == 0); | |
free(ptr_string); | |
StringDestroy(&test1); | |
printf("\n---- SUBSTR ----\n"); | |
StringInit(&test1, "test42 coucou salut"); | |
ptr_string = test1.substr(&test1, 3, 2); | |
testCondition("substr", "Test with basic parameters", strcmp(ptr_string->str, "t4") == 0); | |
ptr_string = test1.substr(&test1, -3, 2); | |
testCondition("substr", "Test with negative offset", strcmp(ptr_string->str, "lu") == 0); | |
ptr_string = test1.substr(&test1, 6, -2); | |
testCondition("substr", "Test with negative length", strcmp(ptr_string->str, "42") == 0); | |
ptr_string = test1.substr(&test1, -6, -2); | |
testCondition("substr", "Test with negative offset and length", strcmp(ptr_string->str, "ou") == 0); | |
ptr_string = test1.substr(&test1, -1, 5); | |
testCondition("substr", "Test with over offset", strcmp(ptr_string->str, "t") == 0); | |
ptr_string = test1.substr(&test1, 2, -5); | |
testCondition("substr", "Test with over offset (with negative length)", strcmp(ptr_string->str, "te") == 0); | |
StringDestroy(&test1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment