usaa18cv3/tests/test.c

70 lines
2.0 KiB
C
Raw Permalink Normal View History

2018-11-13 23:09:02 +00:00
#include "../pizza3.h"
#include "unity.h"
#include <string.h>
#define FILE_NAME "testing_file.txt"
void prepare_file(const char* content){
FILE* f = fopen(FILE_NAME,"w");
fwrite(content,1,strlen(content),f);
fclose(f);
}
void test_return_read_normal_line(){
char content[100] = "first line\nsecond line";
prepare_file(content);
FILE* f = fopen(FILE_NAME,"r");
char buffer[LINE_SIZE];
int res = read_line(f,buffer,LINE_SIZE);
char msg[1000];
sprintf(msg,"Zla navratova hodnota. Obsah suboru: \"%s\"",content);
TEST_ASSERT_GREATER_THAN_INT_MESSAGE(0,res,msg);
TEST_ASSERT_EQUAL_INT_MESSAGE(11,res,msg);
fclose(f);
}
void test_buffer_read_normal_line(){
char content[100] = "first line\nsecond line";
prepare_file(content);
FILE* f = fopen(FILE_NAME,"r");
char buffer[LINE_SIZE];
read_line(f,buffer,LINE_SIZE);
char msg[1000];
sprintf(msg,"Zly obsah buffra. Obsah suboru: \"%s\"",content);
TEST_ASSERT_EQUAL_STRING_MESSAGE("first line\n",buffer,msg);
fclose(f);
}
void test_return_read_unfinished_line(){
char content[100] = "first line";
prepare_file(content);
FILE* f = fopen(FILE_NAME,"r");
char buffer[LINE_SIZE];
int res = read_line(f,buffer,LINE_SIZE);
char msg[1000];
sprintf(msg,"Zla navratova hodnota. Funkcia ma vracat EOF. Obsah suboru: \"%s\"",content);
TEST_ASSERT_EQUAL_INT_MESSAGE(EOF,res,msg);
fclose(f);
}
void test_buffer_read_unfinished_line(){
char content[100] = "first line";
prepare_file(content);
FILE* f = fopen(FILE_NAME,"r");
char buffer[LINE_SIZE];
read_line(f,buffer,LINE_SIZE);
char msg[1000];
sprintf(msg,"Zly obsah buffra. Obsah suboru: \"%s\"",content);
TEST_ASSERT_EQUAL_STRING_MESSAGE("first line",buffer,msg);
fclose(f);
}
int main(){
UNITY_BEGIN();
RUN_TEST(test_return_read_normal_line);
RUN_TEST(test_buffer_read_normal_line);
RUN_TEST(test_return_read_unfinished_line);
RUN_TEST(test_buffer_read_unfinished_line);
return UNITY_END();
}