2fb1498f60995e14dde7add344b9f5722e030ec2
[mono.git] / mono / unit-tests / test-memfuncs.c
1 /*
2  * test-sgen-qsort.c: Unit test for our own bzero/memmove.
3  *
4  * Copyright (C) 2013 Xamarin Inc
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License 2.0 as published by the Free Software Foundation;
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License 2.0 along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "config.h"
21
22 #include "utils/memfuncs.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <assert.h>
28
29 #define POOL_SIZE       2048
30 #define START_OFFSET    128
31
32 #define BZERO_OFFSETS   64
33 #define BZERO_SIZES     256
34
35 #define MEMMOVE_SRC_OFFSETS             32
36 #define MEMMOVE_DEST_OFFSETS            32
37 #define MEMMOVE_SIZES                   256
38 #define MEMMOVE_NONOVERLAP_START        1024
39
40 int
41 main (void)
42 {
43         unsigned char *random_mem = (unsigned char *)malloc (POOL_SIZE);
44         unsigned char *reference = (unsigned char *)malloc (POOL_SIZE);
45         unsigned char *playground = (unsigned char *)malloc (POOL_SIZE);
46         long *long_random_mem;
47         int i, offset, size, src_offset, dest_offset;
48
49         srandom (time (NULL));
50
51         /* init random memory */
52         long_random_mem = (long*)random_mem;
53         for (i = 0; i < POOL_SIZE / sizeof (long); ++i)
54                 long_random_mem [i] = random ();
55
56         /* test bzero */
57         for (offset = 0; offset <= BZERO_OFFSETS; ++offset) {
58                 for (size = 0; size <= BZERO_SIZES; ++size) {
59                         memcpy (reference, random_mem, POOL_SIZE);
60                         memcpy (playground, random_mem, POOL_SIZE);
61
62                         memset (reference + START_OFFSET + offset, 0, size);
63                         mono_gc_bzero_atomic (playground + START_OFFSET + offset, size);
64
65                         assert (!memcmp (reference, playground, POOL_SIZE));
66                 }
67         }
68
69         /* test memmove */
70         for (src_offset = -MEMMOVE_SRC_OFFSETS; src_offset <= MEMMOVE_SRC_OFFSETS; ++src_offset) {
71                 for (dest_offset = -MEMMOVE_DEST_OFFSETS; dest_offset <= MEMMOVE_DEST_OFFSETS; ++dest_offset) {
72                         for (size = 0; size <= MEMMOVE_SIZES; ++size) {
73                                 /* overlapping */
74                                 memcpy (reference, random_mem, POOL_SIZE);
75                                 memcpy (playground, random_mem, POOL_SIZE);
76
77                                 memmove (reference + START_OFFSET + dest_offset, reference + START_OFFSET + src_offset, size);
78                                 mono_gc_memmove_atomic (playground + START_OFFSET + dest_offset, playground + START_OFFSET + src_offset, size);
79
80                                 assert (!memcmp (reference, playground, POOL_SIZE));
81
82                                 /* non-overlapping with dest < src */
83                                 memcpy (reference, random_mem, POOL_SIZE);
84                                 memcpy (playground, random_mem, POOL_SIZE);
85
86                                 memmove (reference + START_OFFSET + dest_offset, reference + MEMMOVE_NONOVERLAP_START + src_offset, size);
87                                 mono_gc_memmove_atomic (playground + START_OFFSET + dest_offset, playground + MEMMOVE_NONOVERLAP_START + src_offset, size);
88
89                                 assert (!memcmp (reference, playground, POOL_SIZE));
90
91                                 /* non-overlapping with dest > src */
92                                 memcpy (reference, random_mem, POOL_SIZE);
93                                 memcpy (playground, random_mem, POOL_SIZE);
94
95                                 memmove (reference + MEMMOVE_NONOVERLAP_START + dest_offset, reference + START_OFFSET + src_offset, size);
96                                 mono_gc_memmove_atomic (playground + MEMMOVE_NONOVERLAP_START + dest_offset, playground + START_OFFSET + src_offset, size);
97
98                                 assert (!memcmp (reference, playground, POOL_SIZE));
99                         }
100                 }
101         }
102
103         return 0;
104 }