[threadpool] Saner default for max number of worker threads on android and ios
[mono-project.git] / mono / unit-tests / test-memfuncs.c
blob2fb1498f60995e14dde7add344b9f5722e030ec2
1 /*
2 * test-sgen-qsort.c: Unit test for our own bzero/memmove.
4 * Copyright (C) 2013 Xamarin Inc
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;
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.
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.
20 #include "config.h"
22 #include "utils/memfuncs.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <assert.h>
29 #define POOL_SIZE 2048
30 #define START_OFFSET 128
32 #define BZERO_OFFSETS 64
33 #define BZERO_SIZES 256
35 #define MEMMOVE_SRC_OFFSETS 32
36 #define MEMMOVE_DEST_OFFSETS 32
37 #define MEMMOVE_SIZES 256
38 #define MEMMOVE_NONOVERLAP_START 1024
40 int
41 main (void)
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;
49 srandom (time (NULL));
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 ();
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);
62 memset (reference + START_OFFSET + offset, 0, size);
63 mono_gc_bzero_atomic (playground + START_OFFSET + offset, size);
65 assert (!memcmp (reference, playground, POOL_SIZE));
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);
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);
80 assert (!memcmp (reference, playground, POOL_SIZE));
82 /* non-overlapping with dest < src */
83 memcpy (reference, random_mem, POOL_SIZE);
84 memcpy (playground, random_mem, POOL_SIZE);
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);
89 assert (!memcmp (reference, playground, POOL_SIZE));
91 /* non-overlapping with dest > src */
92 memcpy (reference, random_mem, POOL_SIZE);
93 memcpy (playground, random_mem, POOL_SIZE);
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);
98 assert (!memcmp (reference, playground, POOL_SIZE));
103 return 0;