File: tst-string.c

package info (click to toggle)
libeconf 0.7.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,644 kB
  • sloc: ansic: 7,483; python: 1,204; cs: 651; sh: 214; xml: 182; makefile: 7
file content (85 lines) | stat: -rw-r--r-- 2,098 bytes parent folder | download | duplicates (3)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "libeconf.h"

/* Test case:
 *  Tests several string format. See tst-arguments-string/etc/arguments.conf.
 */

static int
print_error_get (const char *getgroup, const char *key, econf_err error)
{
  fprintf (stderr, "ERROR: tried to get '%s' from '%s': %s\n",
	   key, getgroup, econf_errString(error));
  return 1;
}

static bool
check_String (econf_file *key_file, const char *group,
	      const char *key, const char *value)
{
  econf_err error;
  char *val_String;

  if ((error = econf_getStringValue(key_file, group, key, &val_String)))
    {
      print_error_get (group, key, error);
      return false;
    }

  if ((value == NULL && val_String != NULL) ||
      (value != NULL && val_String == NULL) ||
      strcmp(val_String, value))
    {
      fprintf (stderr, "ERROR: %s:Expected String:\n'%s'\n, Got:\n'%s'\n",
	       key, value, val_String);
      return false;
    }
  free(val_String);
  return true;
}


int
main(void)
{
  econf_file *key_file = NULL;
  econf_err error;
  int retval = 0;  

  static const struct {
    const char *const key;
    const char *const val;
  } tests[] = {
    { "string_empty", "" },
    { "string_with_spaces", "string with spaces" },
    { "string_escaped_with_leading_and_trailing_spaces", " string with spaces " },
    { "string_with_newlines", "line one\n    line two" },
    { "string_list_multiple_lines", "\n    line one\n    line two" },
    { "string_escaped_with_newlines", "\"line one\n    line two\"" },
    { "string_with_quotes", "\\\"" },
    { "string_with_quotes_v2", "\\\"" }
  };
  unsigned int i;  

  error = econf_readFile (&key_file, TESTSDIR"tst-arguments-string/etc/arguments.conf", "=", "#");
  if (error)
  {
    fprintf (stderr, "ERROR: couldn't read configuration file: %s\n", econf_errString(error));
    return 1;
  }

  for (i = 0; i < sizeof(tests)/sizeof(*tests); i++)
    {
      if (!check_String(key_file, "main", tests[i].key, tests[i].val))
	retval = 1;
    }

  econf_free(key_file);
  return retval;  
}