aboutsummaryrefslogtreecommitdiff
path: root/tests/core/test_memcpy_memcmp.in
blob: fb5dfe6f429fcf4e799f7616caee42de8501ab9e (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
#include <stdio.h>
#include <string.h>
#include <assert.h>

#define MAXX 48
void reset(unsigned char *buffer) {
  for (int i = 0; i < MAXX; i++) buffer[i] = i + 1;
}
void dump(unsigned char *buffer) {
  for (int i = 0; i < MAXX - 1; i++) printf("%2d,", buffer[i]);
  printf("%d\n", buffer[MAXX - 1]);
}
int main() {
  unsigned char buffer[MAXX];
  for (int i = MAXX / 4; i < MAXX - MAXX / 4; i++) {
    for (int j = MAXX / 4; j < MAXX - MAXX / 4; j++) {
      for (int k = 1; k < MAXX / 4; k++) {
        if (i == j) continue;
        if (i < j && i + k > j) continue;
        if (j < i && j + k > i) continue;
        printf("[%d,%d,%d] ", i, j, k);
        reset(buffer);
        memcpy(buffer + i, buffer + j, k);
        dump(buffer);
        assert(memcmp(buffer + i, buffer + j, k) == 0);
        buffer[i + k / 2]++;
        if (buffer[i + k / 2] != 0) {
          assert(memcmp(buffer + i, buffer + j, k) > 0);
        } else {
          assert(memcmp(buffer + i, buffer + j, k) < 0);
        }
        buffer[i + k / 2]--;
        buffer[j + k / 2]++;
        if (buffer[j + k / 2] != 0) {
          assert(memcmp(buffer + i, buffer + j, k) < 0);
        } else {
          assert(memcmp(buffer + i, buffer + j, k) > 0);
        }
      }
    }
  }
  return 0;
}