Subversion Repositories SvarDOS

Rev

Rev 339 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 339 Rev 340
Line 15... Line 15...
15
static size_t randchunkdata(void *file_chunked, const void *file_raw, size_t file_raw_len, int maxchunksz) {
15
static size_t randchunkdata(void *file_chunked, const void *file_raw, size_t file_raw_len, int maxchunksz) {
16
  size_t file_raw_read = 0;
16
  size_t file_raw_read = 0;
17
  size_t file_chunked_len = 0;
17
  size_t file_chunked_len = 0;
18
 
18
 
19
  for (;;) {
19
  for (;;) {
20
    size_t chunklen = (rand() % maxchunksz) + 1;
20
    int chunklen = (rand() % maxchunksz) + 1;
21
    if (file_raw_read + chunklen > file_raw_len) chunklen = file_raw_len - file_raw_read;
21
    if (file_raw_read + chunklen > file_raw_len) chunklen = file_raw_len - file_raw_read;
22
 
22
 
23
    file_chunked_len += sprintf((char *)file_chunked + file_chunked_len, "%x\r\n", chunklen);
23
    file_chunked_len += sprintf((char *)file_chunked + file_chunked_len, "%x\r\n", chunklen);
24
    if (chunklen > 0) memcpy((char *)file_chunked + file_chunked_len, (char *)file_raw + file_raw_read, chunklen);
24
    if (chunklen > 0) memcpy((char *)file_chunked + file_chunked_len, (char *)file_raw + file_raw_read, chunklen);
25
    file_raw_read += chunklen;
25
    file_raw_read += chunklen;
Line 38... Line 38...
38
  fclose(fd);
38
  fclose(fd);
39
}
39
}
40
 
40
 
41
 
41
 
42
int main(int argc, char **argv) {
42
int main(int argc, char **argv) {
43
  static unsigned char file_raw[30000u];     /* original file */
43
  unsigned char *file_raw;        /* original file */
44
  static unsigned char file_chunked[60000u]; /* chunked version */
44
  unsigned char *file_chunked;    /* chunked version */
45
  static unsigned char file_decoded[60000u]; /* after being un-chunked */
45
  unsigned char *file_decoded;    /* after being un-chunked */
46
  size_t file_raw_len;
46
  size_t file_raw_len;
47
  size_t file_chunked_len;
47
  size_t file_chunked_len;
48
  FILE *fd;
48
  FILE *fd;
49
  int trycount;
49
  unsigned int trycount;
50
 
50
 
51
  if ((argc != 2) || (argv[1][0] == '/')) {
51
  if ((argc != 2) || (argv[1][0] == '/')) {
52
    puts("Usage: unchtest <file>");
52
    puts("Usage: unchtest <file>");
53
    return(1);
53
    return(1);
54
  }
54
  }
55
 
55
 
-
 
56
  #define FILESZ_MAX 20000u
-
 
57
 
-
 
58
  file_raw = malloc(FILESZ_MAX);
-
 
59
  file_chunked = malloc(65000u);
-
 
60
  file_decoded = malloc(65000u);
-
 
61
  if ((file_raw == NULL) || (file_chunked == NULL) || (file_decoded == NULL)) {
-
 
62
    puts("ERROR: out of memory");
-
 
63
    return(1);
-
 
64
  }
-
 
65
 
56
  fd = fopen(argv[1], "rb");
66
  fd = fopen(argv[1], "rb");
57
  if (fd == NULL) {
67
  if (fd == NULL) {
58
    puts("ERROR: failed to open file");
68
    puts("ERROR: failed to open file");
59
    return(1);
69
    return(1);
60
  }
70
  }
61
  file_raw_len = fread(file_raw, 1, sizeof(file_raw), fd);
71
  file_raw_len = fread(file_raw, 1, FILESZ_MAX, fd);
62
  fclose(fd);
72
  fclose(fd);
63
 
73
 
64
  printf("Loaded '%s' (%zu bytes)\r\n", argv[1], file_raw_len);
74
  printf("Loaded '%s' (%zu bytes)\r\n", argv[1], file_raw_len);
65
  srand(time(NULL));
75
  srand(time(NULL));
66
 
76
 
67
  for (trycount = 0; trycount < 1000; trycount++) {
77
  for (trycount = 0; trycount < 30000; trycount++) {
68
    size_t bytesprocessed = 0;
78
    size_t bytesprocessed = 0;
69
    size_t file_decoded_len = 0;
79
    size_t file_decoded_len = 0;
70
    int maxchunksz;
80
    int maxchunksz;
71
 
81
 
72
    /* segment file into chunks of random size */
82
    /* segment file into chunks of random size */
73
    maxchunksz = (rand() % 1024) + 1;
83
    maxchunksz = (rand() % 256) + 8;
74
    file_chunked_len = randchunkdata(file_chunked, file_raw, file_raw_len, maxchunksz);
84
    file_chunked_len = randchunkdata(file_chunked, file_raw, file_raw_len, maxchunksz);
75
 
85
 
76
    printf("=== TRY %d (CHUNKS: %d BYTES MAX) ======================\r\n", trycount + 1, maxchunksz);
86
    printf("=== TRY %d (CHUNKS: %u BYTES MAX) ======================\r\n", trycount + 1, maxchunksz);
77
 
87
 
78
    for (;;) {
88
    for (;;) {
79
      size_t bytes;
89
      size_t bytes;
80
      int decodedbytes;
90
      int decodedbytes;
81
      unsigned char buffer[4096];
91
      static unsigned char buffer[4096];
82
 
92
 
-
 
93
      bytes = (rand() % 256) + 1;
83
      bytes = min((rand() % 256) + 1, file_chunked_len - bytesprocessed);
94
      if (bytes > file_chunked_len - bytesprocessed) bytes = file_chunked_len - bytesprocessed;
84
      printf("processing %4zu bytes of chunked data", bytes);
95
      printf("processing %4zu bytes of chunked data", bytes);
85
      memcpy(buffer, file_chunked + bytesprocessed, bytes);
96
      memcpy(buffer, file_chunked + bytesprocessed, bytes);
86
 
97
 
87
      /* decode the chunked version reading random amounts of data and build a decoded version */
98
      /* decode the chunked version reading random amounts of data and build a decoded version */
88
      decodedbytes = unchunk(buffer, bytes);
99
      decodedbytes = unchunk(buffer, bytes);
Line 101... Line 112...
101
      dumpfile("tst-unch.dat", file_decoded, file_decoded_len);
112
      dumpfile("tst-unch.dat", file_decoded, file_decoded_len);
102
      return(1);
113
      return(1);
103
    }
114
    }
104
  }
115
  }
105
 
116
 
106
  printf("OK\r\n");
117
  printf("OK (%u tests)\r\n", trycount);
107
 
118
 
108
  return(0);
119
  return(0);
109
}
120
}