aboutsummaryrefslogtreecommitdiffstats
path: root/tests/tests.c
blob: e13e67f1722e72f668cb652a37f44277c8371f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Sweden - Kungsberget/GI
# T freq bw fec_hi fec_lo mod transmission-mode guard-interval hierarchy
T 738000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
n204' href='#n204'>204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/* ----------------------------------------------------------------------------
   libconfig - A library for processing structured configuration files
   Copyright (C) 2005-2010  Mark A Lindner

   This file is part of libconfig.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, see
   <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------------
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#ifdef _MSC_VER
#define snprintf _snprintf
#endif

#include <libconfig.h>
#include <tinytest.h>

/* ------------------------------------------------------------------------- */

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_TXTFILE_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(!tt_file_exists(input_file) || !tt_file_exists(output_file))
      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(!tt_file_exists(input_file) || !tt_file_exists(error_file))
      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(!tt_file_exists(input_file) || !tt_file_exists(error_file))
      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);
}