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