test_helpers.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2016 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. #include <algorithm>
  17. #include <vector>
  18. #include <android-base/logging.h>
  19. #include <binder/Status.h>
  20. #include <utils/StrongPointer.h>
  21. #include "android/aidl/tests/ITestService.h"
  22. namespace android {
  23. namespace aidl {
  24. namespace tests {
  25. namespace client {
  26. template <typename T, typename U, typename V>
  27. bool RepeatPrimitive(
  28. const android::sp<android::aidl::tests::ITestService>& service,
  29. android::binder::Status(android::aidl::tests::ITestService::*func)(T, V*),
  30. U input) {
  31. V reply;
  32. android::binder::Status status = (*service.*func)(input, &reply);
  33. if (!status.isOk() || input != reply) {
  34. LOG(ERROR) << "Failed to repeat primitive. status=" << status.toString8()
  35. << ".";
  36. return false;
  37. }
  38. return true;
  39. }
  40. template <typename T>
  41. bool ReverseArray(
  42. const android::sp<android::aidl::tests::ITestService>& service,
  43. android::binder::Status(android::aidl::tests::ITestService::*func)(
  44. const std::vector<T>&, std::vector<T>*, std::vector<T>*),
  45. std::vector<T> input) {
  46. std::vector<T> actual_reversed;
  47. std::vector<T> actual_repeated;
  48. android::binder::Status status = (*service.*func)(
  49. input, &actual_repeated, &actual_reversed);
  50. if (!status.isOk()) {
  51. LOG(ERROR) << "Failed to repeat array. status=" << status.toString8()
  52. << ".";
  53. return false;
  54. }
  55. if (input != actual_repeated) {
  56. LOG(ERROR) << "Repeated version of array did not match";
  57. LOG(ERROR) << "input.size()=" << input.size()
  58. << " repeated.size()=" << actual_repeated.size();
  59. return false;
  60. }
  61. std::reverse(input.begin(), input.end());
  62. if (input != actual_reversed) {
  63. LOG(ERROR) << "Reversed version of array did not match";
  64. return false;
  65. }
  66. return true;
  67. }
  68. } // namespace client
  69. } // namespace tests
  70. } // namespace aidl
  71. } // namespace android