/* ------------------------------------------------------------------------- */
static void parse_and_compare(const char *input_file, const char *output_file)
{
config_t cfg;
int ok;
config_init(&cfg);
config_set_include_dir(&cfg, "./testdata");
ok = config_read_file(&cfg, input_file);
if(!ok)
{
printf("error: %s:%d\n", config_error_text(&cfg),
config_error_line(&cfg));
}
TT_ASSERT_TRUE(ok);
remove("temp.cfg");
TT_ASSERT_TRUE(config_write_file(&cfg, "temp.cfg"));
TT_ASSERT_FILE_EQ("temp.cfg", output_file);
remove("temp.cfg");
config_destroy(&cfg);
}
static void parse_file_and_compare_error(const char *input_file,
const char *parse_error)
{
config_t cfg;
char actual_error[128];
char expected_error[128];
config_init(&cfg);
TT_ASSERT_FALSE(config_read_file(&cfg, input_file));
snprintf(expected_error, sizeof(expected_error), "%s:%s",
input_file, parse_error);
snprintf(actual_error, sizeof(actual_error), "%s:%d %s\n",
config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg));
config_destroy(&cfg);
TT_ASSERT_STR_EQ(actual_error, expected_error);
}
static void parse_string_and_compare_error(const char *input_text,
const char *parse_error)
{
config_t cfg;
char actual_error[128];
char expected_error[128];
config_init(&cfg);
TT_ASSERT_FALSE(config_read_string(&cfg, input_text));
snprintf(expected_error, sizeof(expected_error), "(null):%s", parse_error);
snprintf(actual_error, sizeof(actual_error), "%s:%d %s\n",
config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg));
config_destroy(&cfg);
TT_ASSERT_STR_EQ(actual_error, expected_error);
}
static const char *read_file_to_string(const char *file)
{
struct stat stbuf;
FILE *fp;
int size;
char *buf;
size_t r;
TT_ASSERT_INT_EQ(0, stat(file, &stbuf));
size = stbuf.st_size;
buf = (char *)malloc(size + 1);
fp = fopen(file, "rt");
TT_ASSERT_PTR_NOTNULL(fp);
r = fread(buf, 1, size, fp);
fclose(fp);
TT_ASSERT_INT_EQ(size, r);
*(buf + size) = 0;
return(buf);
}
/* ------------------------------------------------------------------------- */
TT_TEST(ParsingAndFormatting)
{
int i;
for(i = 0;; ++i)
{
char input_file[128], output_file[128];
sprintf(input_file, "testdata/input_%d.cfg", i);
sprintf(output_file, "testdata/output_%d.cfg", i);
printf("parsing %s\n", input_file);
if((access(input_file, F_OK) != 0) || (access(output_file, F_OK) != 0))
break;
parse_and_compare(input_file, output_file);
}
}
/* ------------------------------------------------------------------------- */
TT_TEST(ParseInvalidFiles)
{
int i;
for(i = 0;; ++i)
{
char input_file[128], error_file[128];
char error_text[128];
FILE *fp;
sprintf(input_file, "testdata/bad_input_%d.cfg", i);
sprintf(error_file, "testdata/parse_error_%d.txt", i);
if((access(input_file, F_OK) != 0) || (access(error_file, F_OK) != 0))
break;
fp = fopen(error_file, "rt");
TT_ASSERT_PTR_NOTNULL(fp);
TT_ASSERT_PTR_NOTNULL(fgets(error_text, sizeof(error_text), fp));
fclose(fp);
parse_file_and_compare_error(input_file, error_text);
}
}
/* ------------------------------------------------------------------------- */
TT_TEST(ParseInvalidStrings)
{
int i;
for(i = 0;; ++i)
{
char input_file[128], error_file[128];
const char *input_text;
char error_text[128];
FILE *fp;
sprintf(input_file, "testdata/bad_input_%d.cfg", i);
sprintf(error_file, "testdata/parse_error_%d.txt", i);
if((access(input_file, F_OK) != 0) || (access(error_file, F_OK) != 0))
break;
input_text = read_file_to_string(input_file);
fp = fopen(error_file, "rt");
TT_ASSERT_PTR_NOTNULL(fp);
TT_ASSERT_PTR_NOTNULL(fgets(error_text, sizeof(error_text), fp));
fclose(fp);
parse_string_and_compare_error(input_text, error_text);
free((void *)input_text);
}
}
/* ------------------------------------------------------------------------- */
int main(int argc, char **argv)
{
TT_SUITE_START(LibConfigTests);
TT_SUITE_TEST(LibConfigTests, ParsingAndFormatting);
TT_SUITE_TEST(LibConfigTests, ParseInvalidFiles);
TT_SUITE_TEST(LibConfigTests, ParseInvalidStrings);
TT_SUITE_RUN(LibConfigTests);
TT_SUITE_END(LibConfigTests);
return(0);
}
4 | -12/+12 |
2012-02-08 | Add my name to debian copyright. | Jonathan McCrohan | 1 | -2/+3 |
2012-02-08 | Atomic commit. Rollback to libconfig8 packaging names. | Jonathan McCrohan | 10 | -8/+8 |
2012-02-07 | Rename libconfig9 to libconfig8 to aid release team | Jonathan McCrohan | 1 | -14/+14 |
2012-02-07 | Fix "closes typo" | Jonathan McCrohan | 1 | -1/+1 |
2012-02-07 | Revert "Add symbols" | Jonathan McCrohan | 3 | -739/+1 |
2012-02-07 | Revert "Fix b0rked symbol file." | Jonathan McCrohan | 1 | -1/+1 |
2012-02-07 | Revert "Second attempt" | Jonathan McCrohan | 2 | -0/+194 |
2012-01-29 | Second attempt | Jonathan McCrohan | 2 | -194/+0 |
2012-01-29 | Fix b0rked symbol file. | Jonathan McCrohan | 1 | -1/+1 |
2012-01-29 | Add symbols | Jonathan McCrohan | 3 | -1/+739 |
2012-01-26 | add examples properly | Jonathan McCrohan | 1 | -0/+2 |
2012-01-26 | Update changelog | Jonathan McCrohan | 1 | -3/+6 |
2012-01-26 | fix typo | Jonathan McCrohan | 1 | -1/+1 |
2012-01-26 | fix accidental description changes | Jonathan McCrohan | 1 | -2/+2 |
2012-01-26 | more fucking around to please lintian | Jonathan McCrohan | 1 | -5/+2 |
2012-01-25 | Linitan fixes | Jonathan McCrohan | 2 | -3/+2 |
2012-01-25 | Split out debug packages | Jonathan McCrohan | 2 | -1/+6 |
2011-12-31 | Fix debian/rules | Jonathan McCrohan | 1 | -1/+1 |
2011-12-31 | Remove shlibs | Jonathan McCrohan | 1 | -2/+0 |
2011-12-31 | Remove NMU from changelog | Jonathan McCrohan | 1 | -1/+0 |
2011-12-31 | Lintian Fixes | Jonathan McCrohan | 1 | -2/+4 |
2011-12-31 | Remove Makefile patch | Jonathan McCrohan | 1 | -1/+0 |
2011-12-31 | Update Makefiles | Jonathan McCrohan | 10 | -248/+8222 |
2011-12-31 | Updated makefiles | Jonathan McCrohan | 1 | -0/+1 |