Line data Source code
1 : //-----------------------------------------------------------------------------
2 : // LAGraph/src/test/test_Malloc.c: test LAGraph_Malloc and related methods
3 : //-----------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //-----------------------------------------------------------------------------
17 :
18 : #include "LAGraph_test.h"
19 : char msg [LAGRAPH_MSG_LEN] ;
20 :
21 : //-----------------------------------------------------------------------------
22 : // test_malloc
23 : //-----------------------------------------------------------------------------
24 :
25 1 : void test_malloc (void)
26 : {
27 : char msg [LAGRAPH_MSG_LEN] ;
28 1 : OK (LAGraph_Init (msg)) ;
29 :
30 : char *p ;
31 1 : OK (LAGraph_Malloc ((void **) &p, 42, sizeof (char), msg)) ;
32 43 : for (int k = 0 ; k < 42 ; k++)
33 : {
34 42 : p [k] = (char) k ;
35 : }
36 1 : OK (LAGraph_Free ((void **) &p, msg)) ;
37 1 : TEST_CHECK (p == NULL) ;
38 :
39 1 : size_t huge = 1 + SIZE_MAX/2 ;
40 :
41 : if (sizeof (size_t) >= sizeof (uint64_t))
42 : {
43 1 : LAGraph_Malloc ((void **) &p, huge, sizeof (char), msg) ;
44 1 : TEST_CHECK (p == NULL) ; // was FAIL
45 1 : LAGraph_Calloc ((void **) &p, huge, sizeof (char), msg) ;
46 1 : TEST_CHECK (p == NULL) ; // was FAIL
47 : }
48 :
49 1 : OK (LAGraph_Calloc ((void **) &p, 42, sizeof (char), msg)) ;
50 43 : for (int k = 0 ; k < 42 ; k++)
51 : {
52 42 : TEST_CHECK (*p == '\0') ;
53 : }
54 1 : OK (LAGraph_Free ((void **) &p, msg)) ;
55 1 : TEST_CHECK (p == NULL) ;
56 :
57 1 : OK (LAGraph_Free (NULL, NULL)) ;
58 :
59 1 : LAGraph_Calloc_function = NULL ;
60 :
61 1 : OK (LAGraph_Calloc ((void **) &p, 42, sizeof (char), msg)) ;
62 1 : TEST_CHECK (p != NULL) ;
63 43 : for (int k = 0 ; k < 42 ; k++)
64 : {
65 42 : TEST_CHECK (*p == '\0') ;
66 : }
67 :
68 1 : OK (LAGraph_Realloc ((void **) &p, 100, 42, sizeof (char), msg)) ;
69 43 : for (int k = 0 ; k < 42 ; k++)
70 : {
71 42 : TEST_CHECK (*p == '\0') ;
72 : }
73 59 : for (int k = 42 ; k < 100 ; k++)
74 : {
75 58 : p [k] = (char) k ;
76 : }
77 1 : OK (LAGraph_Free ((void **) &p, NULL)) ;
78 1 : TEST_CHECK (p == NULL) ;
79 :
80 1 : OK (LAGraph_Realloc ((void **) &p, 80, 0, sizeof (char), msg)) ;
81 :
82 : if (sizeof (size_t) >= sizeof (uint64_t))
83 : {
84 1 : int s = (LAGraph_Realloc ((void **) &p, huge, 80, sizeof (char), msg)) ;
85 1 : TEST_CHECK (s == GrB_OUT_OF_MEMORY) ; // was FAIL
86 : }
87 :
88 81 : for (int k = 0 ; k < 80 ; k++)
89 : {
90 80 : p [k] = (char) k ;
91 : }
92 :
93 1 : OK (LAGraph_Realloc ((void **) &p, 80, 80, sizeof (char), msg)) ;
94 81 : for (int k = 0 ; k < 80 ; k++)
95 : {
96 80 : TEST_CHECK (p [k] == (char) k) ; // was FAIL
97 : }
98 :
99 1 : LAGraph_Realloc_function = NULL ;
100 :
101 1 : OK (LAGraph_Realloc ((void **) &p, 100, 80, sizeof (char), msg)) ;
102 81 : for (int k = 0 ; k < 80 ; k++)
103 : {
104 80 : TEST_CHECK (p [k] == (char) k) ; // was FAIL
105 : }
106 :
107 1 : OK (LAGraph_Free ((void **) &p, NULL)) ;
108 1 : TEST_CHECK (p == NULL) ;
109 :
110 1 : OK (LAGraph_Finalize (msg)) ;
111 1 : }
112 :
113 : //-----------------------------------------------------------------------------
114 : // TEST_LIST: the list of tasks for this entire test
115 : //-----------------------------------------------------------------------------
116 :
117 : TEST_LIST = {
118 : {"test_malloc", test_malloc},
119 : // no brutal test needed
120 : {NULL, NULL}
121 : };
|