1 /******************************************************************************
2  *
3  *  Copyright 2015 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License
16  *
17  ******************************************************************************/
18 #include "osi/include/allocator.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include <cstring>
23 
24 class AllocatorTest : public ::testing::Test {};
25 
TEST_F(AllocatorTest,test_osi_strndup)26 TEST_F(AllocatorTest, test_osi_strndup) {
27   char str[] = "IloveBluetooth";
28   size_t len = strlen(str);
29   char* copy_str = NULL;
30 
31   // len == 0
32   copy_str = osi_strndup(str, 0);
33   EXPECT_EQ(0, strcmp(copy_str, ""));
34   osi_free(copy_str);
35 
36   // len == strlen(str)
37   copy_str = osi_strndup(str, len);
38   EXPECT_EQ(0, strcmp(str, copy_str));
39   osi_free(copy_str);
40 
41   // len < strlen(str)
42   copy_str = osi_strndup(str, len - 5);
43   EXPECT_EQ(0, strcmp("IloveBlue", copy_str));
44   osi_free(copy_str);
45 
46   // len > strlen(str)
47   copy_str = osi_strndup(str, len + 5);
48   EXPECT_EQ(0, strcmp(str, copy_str));
49   osi_free(copy_str);
50 }
51