⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xdelta3-test.h

📁 This program is free software you can redistribute it and/or modify it under the terms of the GNU G
💻 H
📖 第 1 页 / 共 5 页
字号:
                                                                                \  values[nvalues++] = MAX-ONE;                                                  \  values[nvalues++] = MAX;                                                      \                                                                                \  rbuf = xd3_alloc_output (stream, rbuf);                                       \  dbuf = xd3_alloc_output (stream, dbuf);                                       \                                                                                \  for (i = 0; i < nvalues; i += 1)                                              \    {                                                                           \      const uint8_t *max;                                                       \      const uint8_t *inp;                                                       \      TYPE val;                                                                 \                                                                                \      DOT ();                                                                   \      rbuf->next = 0;                                                           \                                                                                \      if ((ret = xd3_emit_ ## TYPE (stream, & rbuf, values[i])) ||              \	  (ret = xd3_emit_ ## TYPE (stream, & dbuf, values[i])))                \	{                                                                       \	  goto fail;                                                            \	}                                                                       \                                                                                \      inp = rbuf->base;                                                         \      max = rbuf->base + rbuf->next;                                            \                                                                                \      if (rbuf->next != xd3_sizeof_ ## TYPE (values[i]))                        \	{                                                                       \	  ret = XD3_INTERNAL;                                                         \	  goto fail;                                                            \	}                                                                       \                                                                                \      if ((ret = xd3_read_ ## TYPE (stream, & inp, max, & val)))                \	{                                                                       \	  goto fail;                                                            \	}                                                                       \                                                                                \      if (val != values[i])                                                     \	{                                                                       \	  ret = XD3_INTERNAL;                                                         \	  goto fail;                                                            \	}                                                                       \                                                                                \      DOT ();                                                                   \    }                                                                           \                                                                                \  stream->next_in  = dbuf->base;                                                \  stream->avail_in = dbuf->next;                                                \                                                                                \  for (i = 0; i < nvalues; i += 1)                                              \    {                                                                           \      TYPE val;                                                                 \                                                                                \      if ((ret = xd3_decode_ ## TYPE (stream, & val)))                          \        {                                                                       \          goto fail;                                                            \        }                                                                       \                                                                                \      if (val != values[i])                                                     \        {                                                                       \          ret = XD3_INTERNAL;                                                         \          goto fail;                                                            \        }                                                                       \    }                                                                           \                                                                                \  if (stream->avail_in != 0)                                                    \    {                                                                           \      ret = XD3_INTERNAL;                                                             \      goto fail;                                                                \    }                                                                           \                                                                                \ fail:                                                                          \  xd3_free_output (stream, rbuf);                                               \  xd3_free_output (stream, dbuf);                                               \                                                                                \  return retstatic inttest_encode_decode_uint32_t (xd3_stream *stream, int unused){  TEST_ENCODE_DECODE_INTEGER(uint32_t,1U,UINT32_MAX);}static inttest_encode_decode_uint64_t (xd3_stream *stream, int unused){  TEST_ENCODE_DECODE_INTEGER(uint64_t,1ULL,UINT64_MAX);}static inttest_usize_t_overflow (xd3_stream *stream, int unused){  if (USIZE_T_OVERFLOW (0, 0)) { goto fail; }  if (USIZE_T_OVERFLOW (USIZE_T_MAX, 0)) { goto fail; }  if (USIZE_T_OVERFLOW (0, USIZE_T_MAX)) { goto fail; }  if (USIZE_T_OVERFLOW (USIZE_T_MAX / 2, 0)) { goto fail; }  if (USIZE_T_OVERFLOW (USIZE_T_MAX / 2, USIZE_T_MAX / 2)) { goto fail; }  if (USIZE_T_OVERFLOW (USIZE_T_MAX / 2, USIZE_T_MAX / 2 + 1)) { goto fail; }  if (! USIZE_T_OVERFLOW (USIZE_T_MAX, 1)) { goto fail; }  if (! USIZE_T_OVERFLOW (1, USIZE_T_MAX)) { goto fail; }  if (! USIZE_T_OVERFLOW (USIZE_T_MAX / 2 + 1, USIZE_T_MAX / 2 + 1)) { goto fail; }  return 0; fail:  stream->msg = "incorrect overflow computation";  return XD3_INTERNAL;}/****************************************************************************************** Address cache ******************************************************************************************/static inttest_address_cache (xd3_stream *stream, int unused){  int ret, i;  usize_t offset;  usize_t *addrs;  uint8_t *big_buf, *buf_max;  const uint8_t *buf;  xd3_output *outp;  uint8_t *modes;  int mode_counts[16];  stream->acache.s_near = stream->code_table_desc->near_modes;  stream->acache.s_same = stream->code_table_desc->same_modes;  if ((ret = xd3_encode_init (stream))) { return ret; }  addrs = xd3_alloc (stream, sizeof (usize_t), ADDR_CACHE_ROUNDS);  modes = xd3_alloc (stream, sizeof (uint8_t), ADDR_CACHE_ROUNDS);  memset (mode_counts, 0, sizeof (mode_counts));  memset (modes, 0, ADDR_CACHE_ROUNDS);  addrs[0] = 0;  srand (0x9f73f7fc);  /* First pass: encode addresses */  xd3_init_cache (& stream->acache);  for (offset = 1; offset < ADDR_CACHE_ROUNDS; offset += 1)    {      double p;      usize_t addr;      usize_t prev_i;      usize_t nearby;      p         = (rand () / (double)RAND_MAX);      prev_i    = rand () % offset;      nearby    = (rand () % 256) % offset, 1;      nearby    = max (1U, nearby);      if (p < 0.1)      { addr = addrs[offset-nearby]; }      else if (p < 0.4) { addr = min (addrs[prev_i] + nearby, offset-1); }      else              { addr = prev_i; }      if ((ret = xd3_encode_address (stream, addr, offset, & modes[offset]))) { return ret; }      addrs[offset] = addr;      mode_counts[modes[offset]] += 1;    }  /* Copy addresses into a contiguous buffer. */  big_buf = xd3_alloc (stream, xd3_sizeof_output (ADDR_HEAD (stream)), 1);  for (offset = 0, outp = ADDR_HEAD (stream); outp != NULL; offset += outp->next, outp = outp->next_page)    {      memcpy (big_buf + offset, outp->base, outp->next);    }  buf_max = big_buf + offset;  buf     = big_buf;  /* Second pass: decode addresses */  xd3_init_cache (& stream->acache);  for (offset = 1; offset < ADDR_CACHE_ROUNDS; offset += 1)    {      uint32_t addr;      if ((ret = xd3_decode_address (stream, offset, modes[offset], & buf, buf_max, & addr))) { return ret; }      if (addr != addrs[offset])	{	  stream->msg = "incorrect decoded address";	  return XD3_INTERNAL;	}    }  /* Check that every byte, mode was used. */  if (buf != buf_max)    {      stream->msg = "address bytes not used";      return XD3_INTERNAL;    }  for (i = 0; i < (2 + stream->acache.s_same + stream->acache.s_near); i += 1)    {      if (mode_counts[i] == 0)	{	  stream->msg = "address mode not used";	  return XD3_INTERNAL;	}    }  xd3_free (stream, modes);  xd3_free (stream, addrs);  xd3_free (stream, big_buf);  return 0;}/****************************************************************************************** Encode and decode with single bit error ******************************************************************************************//* It compresses from 256 to around 185 bytes. * Avoids matching addresses that are a single-bit difference. * Avoids matching address 0. */static const uint8_t test_text[] ="this is a story\n""abouttttttttttt\n""- his is a stor\n""- about nothing "" all. boutique -""his story is a -""about           ""what happens all"" the time what -""am I ttttttt the"" person said, so"" what, per son -"" gory story is -"" about nothing -""tttttt to test -""his sto nothing";static const uint8_t test_apphead[] = "header test";static inttest_compress_text (xd3_stream  *stream,		    uint8_t     *encoded,		    usize_t     *encoded_size){  int ret;  xd3_config cfg;  int oflags = stream->flags;  int flags = stream->flags | XD3_FLUSH;  xd3_free_stream (stream);  xd3_init_config (& cfg, flags);  /* This configuration is fixed so that the "expected non-error" the counts in   * decompress_single_bit_errors are too.  See test_coftcfg_str. */  cfg.smatch_cfg = XD3_SMATCH_SOFT;  cfg.smatcher_soft.name = "test";  cfg.smatcher_soft.large_look = 64; /* no source, not used */  cfg.smatcher_soft.large_step = 64; /* no source, not used */  cfg.smatcher_soft.small_look = 4;  cfg.smatcher_soft.small_chain = 128;  cfg.smatcher_soft.small_lchain = 16;  cfg.smatcher_soft.max_lazy = 8;  cfg.smatcher_soft.long_enough = 128;    xd3_config_stream (stream, & cfg);  (*encoded_size) = 0;  xd3_set_appheader (stream, test_apphead, sizeof (test_apphead));  if ((ret = xd3_encode_stream (stream, test_text, sizeof (test_text),				encoded, encoded_size, 4*sizeof (test_text)))) { goto fail; }  if ((ret = xd3_close_stream (stream))) { goto fail; } fail:  xd3_free_stream (stream);  xd3_init_config (& cfg, oflags);  xd3_config_stream (stream, & cfg);  return ret;}static inttest_decompress_text (xd3_stream *stream, uint8_t *enc, usize_t enc_size, usize_t test_desize){  xd3_config cfg;  char decoded[sizeof (test_text)];  uint8_t *apphead;  usize_t apphead_size;  usize_t decoded_size;  const char *msg;  int  ret;  usize_t pos = 0;  int flags = stream->flags;  usize_t take; input:  /* Test decoding test_desize input bytes at a time */  take = min (enc_size - pos, test_desize);  CHECK(take > 0);  xd3_avail_input (stream, enc + pos, take); again:  ret = xd3_decode_input (stream);  pos += take;  take = 0;  switch (ret)    {    case XD3_OUTPUT:      break;    case XD3_WINSTART:    case XD3_GOTHEADER:      goto again;    case XD3_INPUT:      if (pos < enc_size) { goto input; }      /* else fallthrough */    case XD3_WINFINISH:    default:      goto fail;    }  CHECK(ret == XD3_OUTPUT);  CHECK(pos == enc_size);  if (stream->avail_out != sizeof (test_text))    {      stream->msg = "incorrect output size";      ret = XD3_INTERNAL;      goto fail;    }  decoded_size = stream->avail_out;  memcpy (decoded, stream->next_out, stream->avail_out);  xd3_consume_output (stream);  if ((ret = xd3_get_appheader (stream, & apphead, & apphead_size))) { goto fail; }  if (apphead_size != sizeof (test_apphead) || memcmp (apphead, test_apphead, sizeof (test_apphead)) != 0)    {      stream->msg = "incorrect appheader";      ret = XD3_INTERNAL;      goto fail;    }  if ((ret = xd3_decode_input (stream)) != XD3_WINFINISH ||      (ret = xd3_close_stream (stream)) != 0)    {      goto fail;    }  if (decoded_size != sizeof (test_text) || memcmp (decoded, test_text, sizeof (test_text)) != 0)    {      stream->msg = "incorrect output text";      ret = EIO;    } fail:  msg = stream->msg;  xd3_free_stream (stream);  xd3_init_config (& cfg, flags);  xd3_config_stream (stream, & cfg);  stream->msg = msg;  return ret;}static inttest_decompress_single_bit_error (xd3_stream *stream, int expected_non_failures){  int ret;  int i;  uint8_t encoded[4*sizeof (test_text)]; /* make room for alt code table */  usize_t  encoded_size;  int non_failures = 0;  int cksum = (stream->flags & XD3_ADLER32) != 0;#if 1#define TEST_FAILURES()#else  /* For checking non-failure cases by hand, enable this macro and run xdelta printdelta   * with print_cpymode enabled.  Every non-failure should change a copy address mode,   * which doesn't cause a failure because the address cache starts out with all zeros.    ./xdelta3 test    for i in test_text.xz.*; do ./xdelta3 printdelta $i > $i.out; diff $i.out test_text.xz.0.out; done   */  system ("rm -rf test_text.*");  {    char buf[TESTBUFSIZE];    FILE *f;    sprintf (buf, "test_text");    f = fopen (buf, "w");    fwrite (test_text,1,sizeof (test_text),f);    fclose (f);  }#define TEST_FAILURES()                                                         \  do {                                                                          \    char buf[TESTBUFSIZE]                                                       \    FILE *f;                                                                    \    sprintf (buf, "test_text.xz.%d", non_failures);                             \    f = fopen (buf, "w");                                                       \    fwrite (encoded,1,encoded_size,f);                                          \    fclose (f);                                                                 \  } while (0)#endif  stream->sec_data.inefficient = 1;  stream->sec_inst.inefficient = 1;  stream->sec_addr.inefficient = 1;  /* Encode text, test correct input */  if ((ret = test_compress_text (stream, encoded, & encoded_size)))    {      /*stream->msg = "without error: encode failure";*/      return ret;    }  if ((ret = test_decompress_text (stream, encoded, encoded_size, sizeof (test_text) / 4)))    {      /*stream->msg = "without error: decode failure";*/      return ret;    }  TEST_FAILURES();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -