1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_fdtostring_tests"
19 #include <log/log.h>
20
21 #include <audio_utils/FdToString.h>
22 #include <gtest/gtest.h>
23
24 using namespace android::audio_utils;
25
TEST(audio_utils_fdtostring,basic)26 TEST(audio_utils_fdtostring, basic) {
27 const std::string PREFIX{"aa "};
28 const std::string TEST_STRING{"hello world"};
29
30 FdToString fdToString(PREFIX);
31 const int fd = fdToString.fd();
32 ASSERT_TRUE(fd >= 0);
33
34 write(fd, TEST_STRING.c_str(), TEST_STRING.size());
35
36 const std::string result = fdToString.getStringAndClose();
37
38 ASSERT_EQ((PREFIX + TEST_STRING), result);
39 }
40
TEST(audio_utils_fdtostring,multilines)41 TEST(audio_utils_fdtostring, multilines) {
42 const std::string PREFIX{"aa "};
43 const std::string DELIM{"\n"};
44 const std::string TEST_STRING1{"hello world\n"};
45 const std::string TEST_STRING2{"goodbye\n"};
46
47 FdToString fdToString(PREFIX);
48 const int fd = fdToString.fd();
49 ASSERT_TRUE(fd >= 0);
50
51 write(fd, TEST_STRING1.c_str(), TEST_STRING1.size());
52 write(fd, DELIM.c_str(), DELIM.size()); // double newline
53 write(fd, TEST_STRING2.c_str(), TEST_STRING2.size());
54
55 const std::string result = fdToString.getStringAndClose();
56
57 ASSERT_EQ((PREFIX + TEST_STRING1 + PREFIX + DELIM + PREFIX + TEST_STRING2), result);
58 }
59