aboutsummaryrefslogtreecommitdiff
path: root/tests/cxx/serializer/reset/driver.cxx
blob: 1e7350a114855ea15c9feab13d2a996ddc16322d (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
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// file      : tests/cxx/serializer/reset/driver.cxx
// copyright : Copyright (c) 2006-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

// Test serializer resetting.
//

#include <assert.h>

#include <string>
#include <sstream>
#include <iostream>

#include "test-sskel.hxx"

using namespace std;
using namespace test;

bool fail = true;

struct error {};

#ifdef XSDE_REUSE_STYLE_MIXIN
struct base_simpl: virtual base_sskel
#else
struct base_simpl: base_sskel
#endif
{
  base_simpl (unsigned long i)
      : i_ (i)
  {
  }

  virtual void
  pre ()
  {
#ifndef XSDE_EXCEPTIONS
    assert (!_error ());
#endif

    if (fail && i_ == 3)
    {
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
    }
  }

private:
  unsigned long i_;
};

#ifdef XSDE_REUSE_STYLE_MIXIN
struct inner_simpl: inner_sskel, base_simpl
#else
struct inner_simpl: inner_sskel
#endif
{
  inner_simpl (unsigned long i)
#ifdef XSDE_REUSE_STYLE_MIXIN
      : base_simpl (i), i_ (i)
#else
          : inner_sskel (&base_impl_), base_impl_ (i), i_ (i)
#endif
  {
  }

  virtual void
  _pre ()
  {
    n_ = 0;
  }

  virtual bool
  b_next ()
  {
    if (fail && i_ == 6)
    {
#ifdef XSDE_SERIALIZER_VALIDATION
      return false;
#else
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
      return true;
#endif
    }

    return n_++ == 0;
  }

  virtual int
  b ()
  {
    if (fail && i_ == 4)
    {
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
    }

    return 1;
  }

  virtual void
  post ()
  {
    if (fail && i_ == 5)
    {
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
    }
  }

private:
#ifdef XSDE_REUSE_STYLE_TIEIN
  base_simpl base_impl_;
#endif
  unsigned long i_;
  unsigned long n_;
};

struct type_simpl: type_sskel
{
  type_simpl (unsigned long i)
      : i_ (i)
  {
  }

  virtual void
  pre ()
  {
    if (fail && i_ == 0)
    {
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
    }
  }

  virtual void
  a ()
  {
    if (fail && i_ == 1)
    {
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
    }
  }

  virtual void
  post ()
  {
    if (fail && i_ == 2)
    {
#ifdef XSDE_EXCEPTIONS
      throw error ();
#else
      _app_error (1);
#endif
    }
  }

private:
  unsigned long i_;
};

int
main ()
{
  try
  {
    for (unsigned long i (0); i < 7; ++i)
    {
      xml_schema::int_simpl int_s;
      inner_simpl inner_s (i);
      type_simpl type_s (i);

      inner_s.serializers (int_s);
      type_s.serializers (inner_s);

      xml_schema::document_simpl doc_s (type_s, "test", "root");

      doc_s.add_prefix ("t", "test");
      doc_s.add_schema ("test", "test.xsd");

      ostringstream ostr;

      cout << i << ": ";

#ifdef XSDE_EXCEPTIONS
      try
      {
        fail = true;

        type_s.pre ();
        doc_s.serialize (ostr);
        type_s.post ();
        assert (false);
      }
      catch (error const&)
      {
      }
      catch (xml_schema::serializer_exception const&)
      {
      }

      fail = false;
      doc_s.reset ();

      type_s.pre ();
      doc_s.serialize (cout);
      type_s.post ();
#else
      do
      {
        fail = true;

        type_s.pre ();

        if (type_s._error ())
          break;

        doc_s.serialize (ostr);

        if (doc_s._error ())
          break;

        type_s.post ();

        if (type_s._error ())
          break;

        assert (false);
      }
      while (false);

      fail = false;
      doc_s.reset ();

      type_s.pre ();
      assert (!type_s._error ());

      doc_s.serialize (cout);
      assert (!doc_s._error ());

      type_s.post ();
      assert (!type_s._error ());
#endif

      cout << endl;
    }
  }
#ifdef XSDE_EXCEPTIONS
  catch (xml_schema::serializer_exception const& e)
  {
    cerr << e << endl;
    return 1;
  }
#endif
  catch (std::ios_base::failure const&)
  {
    cerr << "io failure" << endl;
    return 1;
  }

  return 0;
}