check_xml.c 2.81 KB
Newer Older
1
/* tarot implements the rules of the tarot game
Vivien Kraus's avatar
Vivien Kraus committed
2
 * Copyright (C) 2018, 2019, 2020  Vivien Kraus
Vivien Kraus's avatar
Vivien Kraus committed
3
 *
4
5
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, version 3 of the License.
Vivien Kraus's avatar
Vivien Kraus committed
7
8
9
10
11
12
13
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Vivien Kraus's avatar
Vivien Kraus committed
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
 */

#include <config.h>
#include <tarot/game_event.h>
#include <tarot/game.h>
#include <tarot/xml.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "xalloc.h"
#include <stdalign.h>

static void
unit_test (const char *xml)
{
  char *rebuilt = NULL;
  TarotXmlParser *parser = tarot_xml_parser_alloc (xml);
  TarotXmlParserIterator *iterator;
  TarotGame *game = tarot_game_alloc ();
  const TarotGameEvent *event;
  if (parser == NULL)
    {
      abort ();
    }
  iterator = tarot_xml_parser_iterator (parser);
  if (iterator == NULL)
    {
      abort ();
    }
  while ((event = tarot_xml_parser_iterator_next_value (iterator)) != NULL)
    {
      if (tarot_game_add_event (game, event) != TAROT_GAME_OK)
	{
	  abort ();
	}
    }
51
52
  tarot_xml_parser_iterator_free (iterator);
  tarot_xml_parser_free (parser);
Vivien Kraus's avatar
Vivien Kraus committed
53
54
55
56
57
58
59
60
61
62
  rebuilt = tarot_game_save_to_xml_alloc (game);
  if (rebuilt == NULL)
    {
      abort ();
    }
  if (strcmp (xml, rebuilt) != 0)
    {
      fprintf (stderr, "%s:%d: %s, size %lu\n", __FILE__, __LINE__, rebuilt, strlen (rebuilt));
      abort ();
    }
Vivien Kraus's avatar
Vivien Kraus committed
63
  free (rebuilt);
64
  tarot_game_free (game);
Vivien Kraus's avatar
Vivien Kraus committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
}

static void
push (char **buffer, size_t *available, size_t *used, char c)
{
  if (*available <= *used)
    {
      *available *= 2;
      *buffer = realloc (*buffer, *available);
      if (*buffer == NULL)
	{
	  abort ();
	}
    }
  (*buffer)[(*used)++] = c;
}

static char *
read_all (FILE *file)
{
  char *buffer = NULL;
  size_t available = 1;
  size_t used = 0;
  int c;
  buffer = malloc (1);
  if (buffer == NULL)
    {
      abort ();
    }
  while ((c = fgetc (file)) != EOF)
    {
      push (&buffer, &available, &used, c);
    }
  push (&buffer, &available, &used, '\0');
  return buffer;
}

int
main ()
{
  const char *filename = ABS_TOP_SRCDIR "/src/libtarot/xml/check_xml_1.xml";
  FILE *file = fopen (filename, "r");
  char *buffer = NULL;
  if (file == NULL)
    {
      fprintf (stderr, "'%s' is not present\n", filename);
      abort ();
    }
  buffer = read_all (file);
  fprintf (stderr, "%s:%d: Input: %lu bytes\n", __FILE__, __LINE__, strlen (buffer));
  fclose (file);
  unit_test (buffer);
  free (buffer);
  return 0;
}