(root)/
libredwg-0.13/
test/
unit-testing/
encode_test.c
       1  #define ENCODE_TEST_C
       2  #define _DEFAULT_SOURCE 1
       3  #if defined(__linux__)
       4  #  define _GNU_SOURCE 1 /* for memmem on linux */
       5  #endif
       6  #define _BSD_SOURCE 1
       7  #ifdef __STDC_ALLOC_LIB__
       8  #  define __STDC_WANT_LIB_EXT2__ 1 /* for strdup */
       9  #else
      10  #  define _USE_BSD 1
      11  #endif
      12  
      13  #define IS_ENCODER
      14  #include <stdlib.h>
      15  #include "../../src/common.h"
      16  // CLANG_DIAG_IGNORE (-Wpragma-pack)
      17  #include "encode.c"
      18  // CLANG_DIAG_RESTORE
      19  #include "tests_common.h"
      20  
      21  BITCODE_RL size = SECTION_R13_SIZE;
      22  //Dwg_Section_Type_r13 section_order[SECTION_R13_SIZE] = { 0 };
      23  
      24  static int
      25  find_duplicates (const Dwg_Data *dwg)
      26  {
      27    for (Dwg_Section_Type_r13 i = 0; (unsigned)i < size; i++)
      28      {
      29        Dwg_Section_Type_r13 id = section_order[i];
      30        for (Dwg_Section_Type_r13 j = 0; (unsigned)j < size; j++)
      31          {
      32            if (i == j)
      33              continue;
      34            if (section_order[j] == id && (unsigned)id != size)
      35              {
      36                fail ("find_duplicate %u", (unsigned)id);
      37                section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
      38                return 1;
      39              }
      40          }
      41      }
      42    return 0;
      43  }
      44  
      45  static void
      46  section_reset (const Dwg_Data *dwg)
      47  {
      48    memset (section_order, 0, sizeof section_order);
      49    for (Dwg_Section_Type_r13 i = 0; (unsigned)i < size; i++)
      50      section_order[i] = i;
      51    fprintf (stderr, "---\n");
      52    section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
      53  }
      54  
      55  static void
      56  test_section_find (const Dwg_Data *dwg)
      57  {
      58    unsigned id;
      59    size = SECTION_R13_SIZE;
      60    section_reset (dwg);
      61    for (Dwg_Section_Type_r13 i = 0; (unsigned)i <= size; i++)
      62      {
      63        id = section_find ((Dwg_Section_Type_r13 *)&section_order, size, i);
      64        if (id != (unsigned)i) // 7 not found, returns SECTION_R13_SIZE
      65          {
      66            fail ("section_find %u => %u", (unsigned)i, (unsigned)id);
      67            section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
      68          }
      69      }
      70    id = section_find ((Dwg_Section_Type_r13 *)&section_order, size, size + 1);
      71    if (id != SECTION_R13_SIZE) // not found
      72      {
      73        fail ("section_find %u => %u", size + 1, id);
      74        section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
      75      }
      76  }
      77  
      78  static void
      79  test_section_move_top (const Dwg_Data *dwg)
      80  {
      81    int err = 0;
      82    size = SECTION_R13_SIZE - 1;
      83    section_reset (dwg);
      84    for (Dwg_Section_Type_r13 i = 0; (unsigned)i < size - 1; i++)
      85      {
      86        // without insert
      87        if (section_move_top ((Dwg_Section_Type_r13 *)&section_order, &size, i))
      88          {
      89            err++;
      90            fail ("section_move_top existing %u", (unsigned)i);
      91          }
      92        section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
      93        if (section_order[0] != i)
      94          {
      95            err++;
      96            fail ("section_move_top %u not first", (unsigned)i);
      97          }
      98        if (find_duplicates (dwg))
      99          {
     100            err++;
     101            fail ("duplicates");
     102          }
     103      }
     104    // with insert
     105    if (!section_move_top ((Dwg_Section_Type_r13 *)&section_order, &size, size))
     106      {
     107        err++;
     108        fail ("section_move_top insert");
     109      }
     110    section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
     111    if (!err)
     112      ok ("test_section_move_top");
     113    else
     114      fail ("test_section_move_top");
     115    size = SECTION_R13_SIZE;
     116  }
     117  
     118  static inline unsigned
     119  maxrand (unsigned max)
     120  {
     121    unsigned rnd = (unsigned)rand ();
     122    return rnd % max;
     123  }
     124  
     125  static void
     126  test_section_remove (const Dwg_Data *dwg)
     127  {
     128    int err = 0;
     129    int sz = size - 1;
     130    section_reset (dwg);
     131    for (unsigned i = 0; i < size; i++)
     132      {
     133        Dwg_Section_Type_r13 id = maxrand (size);
     134        if (section_remove ((Dwg_Section_Type_r13 *)&section_order, &size, id))
     135          sz--;
     136        section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
     137        if (find_duplicates (dwg))
     138          {
     139            err++;
     140            fail ("test_section_remove");
     141          }
     142      }
     143    if (!err)
     144      ok ("test_section_remove");
     145    else
     146      fail ("test_section_remove");
     147  }
     148  
     149  static void test_section_move_before(const Dwg_Data *dwg) {
     150    int err = 0;
     151    size = SECTION_R13_SIZE - 2;
     152    section_reset (dwg);
     153    for (unsigned i = 0; i < size; i++)
     154      {
     155        Dwg_Section_Type_r13 id = maxrand(size);
     156        Dwg_Section_Type_r13 before = maxrand(size);
     157        while (before == id)
     158          before = maxrand(size);
     159        if (section_move_before ((Dwg_Section_Type_r13 *)&section_order,
     160                                 &size, id, before))
     161          {
     162            fail ("move_before %u", (unsigned)id);
     163            err++;
     164          }
     165        section_order_trace (dwg, size, (Dwg_Section_Type_r13 *)&section_order);
     166        err += find_duplicates (dwg);
     167      }
     168    if (!section_move_before ((Dwg_Section_Type_r13 *)&section_order,
     169                              &size, 6, 4))
     170      {
     171        fail ("move_before %u inserts", 6);
     172        err++;
     173      }
     174    err += find_duplicates (dwg);
     175    if (!err)
     176      ok ("test_section_move_before");
     177    else
     178      fail ("test_section_move_before");
     179    size = SECTION_R13_SIZE;
     180  }
     181  
     182  int
     183  main (int argc, char const *argv[])
     184  {
     185    Dwg_Data dwg;
     186    loglevel = is_make_silent () ? 0 : 3;
     187    dwg.header.version = R_2000;
     188  
     189    test_section_find (&dwg);
     190    test_section_move_top (&dwg);
     191    //loglevel = is_make_silent () ? 0 : 3;
     192    test_section_remove (&dwg);
     193    test_section_move_before (&dwg);
     194  
     195    return failed;
     196  }