fdtostring_tests.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_fdtostring_tests"
  18. #include <log/log.h>
  19. #include <audio_utils/FdToString.h>
  20. #include <gtest/gtest.h>
  21. using namespace android::audio_utils;
  22. TEST(audio_utils_fdtostring, basic) {
  23. const std::string PREFIX{"aa "};
  24. const std::string TEST_STRING{"hello world"};
  25. FdToString fdToString(PREFIX);
  26. const int fd = fdToString.fd();
  27. ASSERT_TRUE(fd >= 0);
  28. write(fd, TEST_STRING.c_str(), TEST_STRING.size());
  29. const std::string result = fdToString.getStringAndClose();
  30. ASSERT_EQ((PREFIX + TEST_STRING), result);
  31. }
  32. TEST(audio_utils_fdtostring, multilines) {
  33. const std::string PREFIX{"aa "};
  34. const std::string DELIM{"\n"};
  35. const std::string TEST_STRING1{"hello world\n"};
  36. const std::string TEST_STRING2{"goodbye\n"};
  37. FdToString fdToString(PREFIX);
  38. const int fd = fdToString.fd();
  39. ASSERT_TRUE(fd >= 0);
  40. write(fd, TEST_STRING1.c_str(), TEST_STRING1.size());
  41. write(fd, DELIM.c_str(), DELIM.size()); // double newline
  42. write(fd, TEST_STRING2.c_str(), TEST_STRING2.size());
  43. const std::string result = fdToString.getStringAndClose();
  44. ASSERT_EQ((PREFIX + TEST_STRING1 + PREFIX + DELIM + PREFIX + TEST_STRING2), result);
  45. }