Skip to content

Commit

Permalink
Merge branch 'add_testcase001' of https://github.com/hityc2019/iniparser
Browse files Browse the repository at this point in the history
 into hityc2019-add_testcase001

# Conflicts:
#	test/test_iniparser.c
  • Loading branch information
lmoellendorf committed Mar 2, 2024
2 parents 5f9e0e1 + 0a71800 commit ad2969f
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ clean veryclean:
rm -rf AllTests.c
rm -rf $(OBJ) AllTests.o
rm -rf testrun
rm -rf ./ressources/new.ini ./ressources/test.ini ./ressources/test.txt
4 changes: 4 additions & 0 deletions test/ressources/old.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[section]
key_01 = hello world ;test ini

key1 = 321abc
139 changes: 139 additions & 0 deletions test/test_iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

#define GOOD_INI_PATH "ressources/good_ini"
#define BAD_INI_PATH "ressources/bad_ini"
#define INI_PATH "ressources/old.ini"
#define INI_PATH_1 "ressources/new.ini"
#define INI_PATH_2 "ressources/test.ini"
#define INI_PATH_3 "ressources/test.txt"

#define stringify_2(x) #x
#define stringify(x) stringify_2(x)
Expand Down Expand Up @@ -824,3 +828,138 @@ void Test_iniparser_error_callback(CuTest *tc)
CuAssertPtrEquals(tc, NULL, dic);
CuAssertStrEquals(tc, "", _last_error);
}

void Test_iniparser_dump(CuTest *tc)
{
dictionary *dic;
FILE *fp = NULL;
char buff[255];

/*loading old.ini*/
dic = iniparser_load(INI_PATH);
if(dic ==NULL)
{
printf("error: old.ini is not exist");
exit(-1);
}
/*check the data of old.ini*/
CuAssertStrEquals(tc,"hello world",iniparser_getstring(dic,"section:key_01",NULL));
CuAssertStrEquals(tc,"321abc",iniparser_getstring(dic,"section:key1",NULL));
/*open test.txt*/
fp = fopen(INI_PATH_3,"w");
if(fp == NULL)
{
printf("error: test.txt is not exist");
exit(-1);
}
/*dump the data of old.ini to new.ini*/
iniparser_dump(dic,fp);
fclose(fp);
iniparser_freedict(dic);
/*read the data of test.txt*/
fp = fopen(INI_PATH_3,"r");
if(fp == NULL)
{
printf("error: test.txt is not exist");
exit(-1);
}
fgets(buff,100,fp);
/*remove '\n'*/
if(buff[strlen(buff)-1] == '\n')
{
buff[strlen(buff)-1] = '\0';
}
CuAssertStrEquals(tc,"[section]=UNDEF",buff);
fgets(buff,100,fp);
if(buff[strlen(buff)-1] == '\n')
{
buff[strlen(buff)-1] = '\0';
}
CuAssertStrEquals(tc,"[section:key_01]=[hello world]",buff);
fgets(buff,100,fp);
if(buff[strlen(buff)-1] == '\n')
{
buff[strlen(buff)-1] = '\0';
}
CuAssertStrEquals(tc,"[section:key1]=[321abc]",buff);
fclose(fp);
}

void Test_iniparser_dump_ini(CuTest *tc)
{
dictionary *dic;
FILE *fp = NULL;

/*loading old.ini*/
dic = iniparser_load(INI_PATH);
if(dic ==NULL)
{
printf("error: old.ini is not exist");
exit(-1);
}
/*check the data of old.ini*/
CuAssertStrEquals(tc,"hello world",iniparser_getstring(dic,"section:key_01",NULL));
CuAssertStrEquals(tc,"321abc",iniparser_getstring(dic,"section:key1",NULL));
/*open new.ini*/
fp = fopen(INI_PATH_1,"w");
if(fp == NULL)
{
printf("error: new.ini is not exist");
exit(-1);
}
/*dump the data of old.ini to new.ini*/
iniparser_dump_ini(dic,fp);
fclose(fp);
iniparser_freedict(dic);
/*loading new.ini*/
dic = iniparser_load(INI_PATH_1);
if(dic ==NULL)
{
printf("error: new.ini is not exist");
exit(-1);
}
/*check the data of new.ini*/
CuAssertStrEquals(tc,"hello world",iniparser_getstring(dic,"section:key_01",NULL));
CuAssertStrEquals(tc,"321abc",iniparser_getstring(dic,"section:key1",NULL));
iniparser_freedict(dic);
}

void Test_iniparser_dumpsection_ini(CuTest *tc)
{
dictionary *dic;
FILE *fp = NULL;

/*loading old.ini*/
dic = iniparser_load(INI_PATH);
if(dic ==NULL)
{
printf("error: old.ini is not exist");
exit(-1);
}
/*check the data of old.ini*/
CuAssertStrEquals(tc,"hello world",iniparser_getstring(dic,"section:key_01",NULL));
CuAssertStrEquals(tc,"321abc",iniparser_getstring(dic,"section:key1",NULL));
/*open test.ini*/
fp = fopen(INI_PATH_2,"w");
if(fp == NULL)
{
printf("error: test.ini is not exist");
exit(-1);
}
/*dump the data of old.ini to test.ini*/
iniparser_dumpsection_ini(dic,"section",fp);
fclose(fp);
iniparser_freedict(dic);
/*loading test.ini*/
dic = iniparser_load(INI_PATH_2);
if(dic ==NULL)
{
printf("error: test.ini is not exist");
exit(-1);
}
/*check the data of test.ini*/
CuAssertStrEquals(tc,"hello world",iniparser_getstring(dic,"section:key_01",NULL));
CuAssertStrEquals(tc,"321abc",iniparser_getstring(dic,"section:key1",NULL));
iniparser_freedict(dic);
}

0 comments on commit ad2969f

Please sign in to comment.