File: test_misc.cpp

package info (click to toggle)
boolector 3.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,744 kB
  • sloc: ansic: 83,136; cpp: 18,159; sh: 3,668; python: 2,889; makefile: 210
file content (407 lines) | stat: -rw-r--r-- 12,234 bytes parent folder | download
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*  Boolector: Satisfiability Modulo Theories (SMT) solver.
 *
 *  Copyright (C) 2007-2021 by the authors listed in the AUTHORS file.
 *
 *  This file is part of Boolector.
 *  See COPYING for more information on using this software.
 */

#include "test.h"

extern "C" {
#include "boolector.h"
#include "utils/btormem.h"
#include "utils/btorutil.h"
}

class TestMisc : public TestMm
{
 protected:
  static constexpr uint32_t BTOR_TEST_MISC_LOW  = 1;
  static constexpr uint32_t BTOR_TEST_MISC_HIGH = 4;
  static constexpr uint32_t UEXT                = 0;
  static constexpr uint32_t SEXT                = 1;

  char *int_to_str (int32_t x, int32_t num_bits)
  {
    assert (x >= 0);
    assert (num_bits > 0);
    char *result = nullptr;
    int32_t i    = 0;
    result = (char *) btor_mem_malloc (d_mm, sizeof (char) * (num_bits + 1));
    for (i = num_bits - 1; i >= 0; i--)
    {
      result[i] = x % 2 ? '1' : '0';
      x >>= 1;
    }
    result[num_bits] = '\0';
    return result;
  }

  char *mk_slice (int32_t x, int32_t high, int32_t low, int32_t num_bits)
  {
    assert (high < num_bits);
    assert (low >= 0);
    assert (low <= high);
    char *temp      = nullptr;
    char *result    = nullptr;
    int32_t i       = 0;
    int32_t counter = 0;
    temp = int_to_str (x, num_bits);
    assert (temp != nullptr);
    result  = int_to_str (0, high - low + 1);
    counter = high - low;
    for (i = low; i <= high; i++) result[counter--] = temp[num_bits - 1 - i];
    btor_mem_freestr (d_mm, temp);
    return result;
  }

  void slice_test_misc (int32_t low, int32_t high, uint32_t rwl)
  {
    assert (low > 0);
    assert (low <= high);

    Btor *btor;
    int32_t i        = 0;
    int32_t j        = 0;
    char *result     = 0;
    int32_t num_bits = 0;
    const int32_t x  = 11;

    for (num_bits = low; num_bits <= high; num_bits++)
    {
      for (i = num_bits - 1; i >= 0; i--)
      {
        for (j = i; j >= 0; j--)
        {
          BoolectorSort sort;
          BoolectorNode *const1, *const2, *slice, *eq;

          btor = boolector_new ();
          boolector_set_opt (btor, BTOR_OPT_REWRITE_LEVEL, rwl);

          result = mk_slice (x, i, j, num_bits);

          sort   = boolector_bitvec_sort (btor, high);
          const1 = boolector_unsigned_int (btor, x, sort);
          slice  = boolector_slice (btor, const1, i, j);
          const2 = boolector_const (btor, result);
          eq     = boolector_eq (btor, slice, const2);
          boolector_assert (btor, eq);

          ASSERT_EQ (boolector_sat (btor), BOOLECTOR_SAT);
          boolector_release_sort (btor, sort);
          boolector_release (btor, const1);
          boolector_release (btor, const2);
          boolector_release (btor, slice);
          boolector_release (btor, eq);
          boolector_delete (btor);
          btor_mem_freestr (d_mm, result);
        }
      }
    }
  }

  char *uext (int32_t x, int32_t y, int32_t num_bits)
  {
    assert (x >= 0);
    assert (y >= 0);
    assert (num_bits >= 1);
    char *result = nullptr;
    result = int_to_str (x, num_bits + y);
    return result;
  }

  char *sext (int32_t x, int32_t y, int32_t num_bits)
  {
    assert (x >= 0);
    assert (y >= 0);
    assert (num_bits >= 1);
    char *result = nullptr;
    int32_t i    = 0;
    result = int_to_str (x, num_bits + y);
    if (result[y] == '1')
    {
      for (i = 0; i < y; i++) result[i] = '1';
    }
    return result;
  }

  void ext_test_misc (uint32_t ext_mode,
                      int32_t low,
                      int32_t high,
                      uint32_t rwl)
  {
    assert (ext_mode == UEXT || ext_mode == SEXT);
    assert (low > 0);
    assert (low <= high);

    Btor *btor;
    BoolectorNode *(*btor_fun) (Btor *, BoolectorNode *, uint32_t);

    int32_t i        = 0;
    int32_t j        = 0;
    int32_t max      = 0;
    char *result     = 0;
    int32_t num_bits = 0;

    btor_fun = ext_mode == UEXT ? boolector_uext : boolector_sext;

    for (num_bits = low; num_bits <= high; num_bits++)
    {
      max = btor_util_pow_2 (num_bits);
      for (i = 0; i < max; i++)
      {
        for (j = 0; j < num_bits; j++)
        {
          BoolectorSort sort;
          BoolectorNode *const1, *const2, *eq, *bfun;

          btor = boolector_new ();
          boolector_set_opt (btor, BTOR_OPT_REWRITE_LEVEL, rwl);

          result =
              ext_mode == UEXT ? uext (i, j, num_bits) : sext (i, j, num_bits);

          sort   = boolector_bitvec_sort (btor, num_bits);
          const1 = boolector_unsigned_int (btor, i, sort);
          bfun   = btor_fun (btor, const1, j);
          const2 = boolector_const (btor, result);
          eq     = boolector_eq (btor, bfun, const2);
          boolector_assert (btor, eq);

          ASSERT_EQ (boolector_sat (btor), BOOLECTOR_SAT);
          boolector_release_sort (btor, sort);
          boolector_release (btor, const1);
          boolector_release (btor, const2);
          boolector_release (btor, bfun);
          boolector_release (btor, eq);
          boolector_delete (btor);
          btor_mem_freestr (d_mm, result);
        }
      }
    }
  }

  char *mk_concat (int32_t x, int32_t y, int32_t num_bits)
  {
    assert (x >= 0);
    assert (y >= 0);
    assert (num_bits > 0);
    assert (num_bits <= INT32_MAX / 2);

    char *x_string = nullptr;
    char *y_string = nullptr;
    char *result   = nullptr;

    x_string = int_to_str (x, num_bits);
    y_string = int_to_str (y, num_bits);
    result =
        (char *) btor_mem_malloc (d_mm, sizeof (char) * ((num_bits << 1) + 1));
    strcpy (result, x_string);
    strcat (result, y_string);
    btor_mem_freestr (d_mm, x_string);
    btor_mem_freestr (d_mm, y_string);
    return result;
  }

  void concat_test_misc (int32_t low, int32_t high, uint32_t rwl)
  {
    assert (low > 0);
    assert (low <= high);

    Btor *btor;
    int32_t i        = 0;
    int32_t j        = 0;
    int32_t max      = 0;
    char *result     = 0;
    int32_t num_bits = 0;

    for (num_bits = low; num_bits <= high; num_bits++)
    {
      max = btor_util_pow_2 (num_bits);
      for (i = 0; i < max; i++)
      {
        for (j = 0; j < max; j++)
        {
          BoolectorSort sort;
          BoolectorNode *const1, *const2, *const3, *eq, *concat;

          btor = boolector_new ();
          boolector_set_opt (btor, BTOR_OPT_REWRITE_LEVEL, rwl);

          result = mk_concat (i, j, num_bits);

          sort   = boolector_bitvec_sort (btor, num_bits);
          const1 = boolector_unsigned_int (btor, i, sort);
          const2 = boolector_unsigned_int (btor, j, sort);
          concat = boolector_concat (btor, const1, const2);
          const3 = boolector_const (btor, result);
          eq     = boolector_eq (btor, concat, const3);
          boolector_assert (btor, eq);

          ASSERT_EQ (boolector_sat (btor), BOOLECTOR_SAT);
          boolector_release_sort (btor, sort);
          boolector_release (btor, const1);
          boolector_release (btor, const2);
          boolector_release (btor, const3);
          boolector_release (btor, concat);
          boolector_release (btor, eq);
          boolector_delete (btor);
          btor_mem_freestr (d_mm, result);
        }
      }
    }
  }

  static void cond_test_misc (int32_t low, int32_t high, uint32_t rwl)
  {
    assert (low > 0);
    assert (low <= high);

    Btor *btor;
    int32_t i        = 0;
    int32_t j        = 0;
    int32_t k        = 0;
    int32_t max      = 0;
    int32_t result   = 0;
    int32_t num_bits = 0;

    for (num_bits = low; num_bits <= high; num_bits++)
    {
      max = btor_util_pow_2 (num_bits);
      for (i = 0; i < max; i++)
      {
        for (j = 0; j < max; j++)
        {
          for (k = 0; k <= 1; k++)
          {
            BoolectorSort sort, sort1;
            BoolectorNode *const1, *const2, *const3, *const4, *eq, *cond;

            btor = boolector_new ();
            boolector_set_opt (btor, BTOR_OPT_REWRITE_LEVEL, rwl);

            result = k ? i : j;

            sort   = boolector_bitvec_sort (btor, num_bits);
            sort1  = boolector_bitvec_sort (btor, 1);
            const1 = boolector_unsigned_int (btor, i, sort);
            const2 = boolector_unsigned_int (btor, j, sort);
            const3 = boolector_unsigned_int (btor, k, sort1);
            cond   = boolector_cond (btor, const3, const1, const2);
            const4 = boolector_unsigned_int (btor, result, sort);
            eq     = boolector_eq (btor, cond, const4);
            boolector_assert (btor, eq);

            ASSERT_EQ (boolector_sat (btor), BOOLECTOR_SAT);
            boolector_release_sort (btor, sort);
            boolector_release_sort (btor, sort1);
            boolector_release (btor, const1);
            boolector_release (btor, const2);
            boolector_release (btor, const3);
            boolector_release (btor, const4);
            boolector_release (btor, cond);
            boolector_release (btor, eq);
            boolector_delete (btor);
          }
        }
      }
    }
  }

  void read_test_misc (int32_t low, int32_t high, uint32_t rwl)
  {
    assert (low > 0);
    assert (low <= high);

    Btor *btor;
    int32_t i        = 0;
    int32_t j        = 0;
    int32_t max      = 0;
    int32_t num_bits = 0;

    for (num_bits = low; num_bits <= high; num_bits++)
    {
      max = btor_util_pow_2 (num_bits);
      for (i = 0; i < max; i++)
      {
        for (j = 0; j < max; j++)
        {
          BoolectorSort elem_sort, index_sort, array_sort;
          BoolectorNode *const1, *const2, *const3, *const4;
          BoolectorNode *eq1, *eq2, *array, *read1, *read2;

          btor = boolector_new ();
          boolector_set_opt (btor, BTOR_OPT_REWRITE_LEVEL, rwl);

          elem_sort  = boolector_bitvec_sort (btor, num_bits);
          index_sort = boolector_bitvec_sort (btor, 1);
          array_sort = boolector_array_sort (btor, index_sort, elem_sort);
          array      = boolector_array (btor, array_sort, "array");
          const1     = boolector_false (btor);
          const2     = boolector_true (btor);
          const3     = boolector_unsigned_int (btor, i, elem_sort);
          const4     = boolector_unsigned_int (btor, j, elem_sort);
          read1      = boolector_read (btor, array, const1);
          read2      = boolector_read (btor, array, const2);
          eq1        = boolector_eq (btor, const3, read1);
          eq2        = boolector_eq (btor, const4, read2);
          boolector_assert (btor, eq1);
          boolector_assert (btor, eq2);

          ASSERT_EQ (boolector_sat (btor), BOOLECTOR_SAT);
          boolector_release_sort (btor, elem_sort);
          boolector_release_sort (btor, index_sort);
          boolector_release_sort (btor, array_sort);
          boolector_release (btor, eq1);
          boolector_release (btor, eq2);
          boolector_release (btor, read1);
          boolector_release (btor, read2);
          boolector_release (btor, const1);
          boolector_release (btor, const2);
          boolector_release (btor, const3);
          boolector_release (btor, const4);
          boolector_release (btor, array);
          boolector_delete (btor);
        }
      }
    }
  }
};

TEST_F (TestMisc, slice)
{
  slice_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 1);
  slice_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 0);
}

TEST_F (TestMisc, uext)
{
  ext_test_misc (UEXT, BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 1);
  ext_test_misc (UEXT, BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 0);
}

TEST_F (TestMisc, sext)
{
  ext_test_misc (SEXT, BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 1);
  ext_test_misc (SEXT, BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 0);
}

TEST_F (TestMisc, concat)
{
  concat_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 1);
  concat_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 0);
}

TEST_F (TestMisc, cond)
{
  cond_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 1);
  cond_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 0);
}

TEST_F (TestMisc, read)
{
  read_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 1);
  read_test_misc (BTOR_TEST_MISC_LOW, BTOR_TEST_MISC_HIGH, 0);
}