Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / c_common / oic_malloc / test / linux / oic_malloc_tests.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 extern "C" {
23     #include "oic_malloc.h"
24 }
25
26 #include "gtest/gtest.h"
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34
35 //-----------------------------------------------------------------------------
36 // Includes
37 //-----------------------------------------------------------------------------
38 #include <stdio.h>
39 #include <string.h>
40
41 #include <iostream>
42 #include <stdint.h>
43 using namespace std;
44
45 //-----------------------------------------------------------------------------
46 // Private variables
47 //-----------------------------------------------------------------------------
48 static uint8_t *pBuffer;
49
50 //-----------------------------------------------------------------------------
51 //  Tests
52 //-----------------------------------------------------------------------------
53
54 TEST(OICMalloc, MallocPass1)
55 {
56     // Try to allocate a small buffer
57     pBuffer = (uint8_t *)OICMalloc(1);
58     EXPECT_TRUE(pBuffer);
59     OICFree(pBuffer);
60 }
61
62 TEST(OICMalloc, MallocPass2)
63 {
64     // Try to allocate a small buffer
65     pBuffer = (uint8_t *)OICMalloc(128);
66     EXPECT_TRUE(pBuffer);
67     OICFree(pBuffer);
68 }
69
70 TEST(OICMalloc, MallocFail1)
71 {
72     // Try to allocate a buffer of size 0
73     pBuffer = (uint8_t *)OICMalloc(0);
74     EXPECT_TRUE(NULL == pBuffer);
75     OICFree(pBuffer);
76 }
77
78 TEST(OICMalloc, MallocFail2)
79 {
80     // Try to allocate a ridiculous amount of RAM
81     pBuffer = (uint8_t *)OICMalloc((size_t)0x7FFFFFFFFFFFFFFF);
82     EXPECT_TRUE(NULL == pBuffer);
83     OICFree(pBuffer);
84 }
85
86 TEST(OICCalloc, CallocPass1)
87 {
88     // Try to allocate a small buffer
89     pBuffer = (uint8_t *)OICCalloc(1, 1);
90     EXPECT_TRUE(pBuffer);
91     OICFree(pBuffer);
92 }
93
94 TEST(OICCalloc, CallocPass2)
95 {
96     // Try to allocate a small buffer
97     pBuffer = (uint8_t *)OICCalloc(1, 128);
98     EXPECT_TRUE(pBuffer);
99     OICFree(pBuffer);
100 }
101
102 TEST(OICCalloc, CallocPass3)
103 {
104     // Try to allocate a buffer for an array
105     pBuffer = (uint8_t *)OICCalloc(5, 128);
106     EXPECT_TRUE(pBuffer);
107     OICFree(pBuffer);
108 }
109
110 TEST(OICCalloc, CallocFail1)
111 {
112     // Try to allocate a buffer of size 0
113     pBuffer = (uint8_t *)OICCalloc(1, 0);
114     EXPECT_TRUE(NULL == pBuffer);
115     OICFree(pBuffer);
116 }
117
118 TEST(OICCalloc, CallocFail2)
119 {
120     // Try to allocate a buffer with num of 0
121     pBuffer = (uint8_t *)OICCalloc(0, 5);
122     EXPECT_TRUE(NULL == pBuffer);
123     OICFree(pBuffer);
124 }
125
126 TEST(OICCalloc, CallocFail3)
127 {
128     // Try to allocate a buffer with size and num 0
129     pBuffer = (uint8_t *)OICCalloc(0, 0);
130     EXPECT_TRUE(NULL == pBuffer);
131     OICFree(pBuffer);
132 }
133
134 TEST(OICCalloc, CallocFail4)
135 {
136     // Try to allocate a ridiculous amount of RAM
137     pBuffer = (uint8_t *)OICCalloc(1, (size_t)0x7FFFFFFFFFFFFFFF);
138     EXPECT_TRUE(NULL == pBuffer);
139     OICFree(pBuffer);
140 }