1 /******************************************************************************
2  *
3  *  Copyright (C) 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 <cstring>
19 
20 #include <gtest/gtest.h>
21 
22 #include "AllocationTestHarness.h"
23 
24 extern "C" {
25 #include "osi/include/allocator.h"
26 }
27 
28 class AllocatorTest : public AllocationTestHarness {};
29 
TEST_F(AllocatorTest,test_osi_strndup)30 TEST_F(AllocatorTest, test_osi_strndup) {
31   char str[] = "IloveBluetooth";
32   size_t len = strlen(str);
33   char *copy_str = NULL;
34 
35   // len == 0
36   copy_str = osi_strndup(str, 0);
37   EXPECT_EQ(0, strcmp(copy_str, ""));
38   osi_free(copy_str);
39 
40   // len == strlen(str)
41   copy_str = osi_strndup(str, len);
42   EXPECT_EQ(0, strcmp(str, copy_str));
43   osi_free(copy_str);
44 
45   // len < strlen(str)
46   copy_str = osi_strndup(str, len - 5);
47   EXPECT_EQ(0, strcmp("IloveBlue", copy_str));
48   osi_free(copy_str);
49 
50   // len > strlen(str)
51   copy_str = osi_strndup(str, len + 5);
52   EXPECT_EQ(0, strcmp(str, copy_str));
53   osi_free(copy_str);
54 }
55