xref: /aosp_15_r20/hardware/interfaces/vibrator/aidl/vts/persistable_bundle_utils.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2024 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 #ifndef VIBRATOR_HAL_PERSISTABLE_BUNDLE_UTILS_H
17 #define VIBRATOR_HAL_PERSISTABLE_BUNDLE_UTILS_H
18 
19 #include <android/persistable_bundle_aidl.h>
20 
21 #include <cstdlib>
22 
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace vibrator {
27 namespace testing {
28 
29 using aidl::android::os::PersistableBundle;
30 
31 namespace {
32 
33 template <typename T>
nextValue()34 T nextValue() {
35     return static_cast<T>(std::rand());
36 }
37 
38 template <>
nextValue()39 std::string nextValue() {
40     std::string str;
41     uint8_t entryCount = nextValue<uint8_t>();
42     for (uint8_t i = 0; i < entryCount; i++) {
43         str.push_back(nextValue<char>());
44     }
45     return str;
46 }
47 
48 template <typename T>
nextValue(T limit)49 T nextValue(T limit) {
50     assert(limit > 0);
51     return static_cast<T>(std::rand()) / (static_cast<T>(RAND_MAX / limit));
52 }
53 
54 template <typename T>
fillVector(std::vector<T> * values)55 void fillVector(std::vector<T>* values) {
56     uint8_t entryCount = nextValue<uint8_t>();
57     for (uint8_t i = 0; i < entryCount; i++) {
58         values->push_back(nextValue<T>());
59     }
60 }
61 
62 const std::vector<std::function<void(PersistableBundle*, const std::string&)>>
63         sPersistableBundleSetters = {[](PersistableBundle* bundle, const std::string& key) -> void {
64                                          bundle->putBoolean(key, nextValue<bool>());
65                                      },
66                                      [](PersistableBundle* bundle, const std::string& key) -> void {
67                                          bundle->putInt(key, nextValue<int32_t>());
68                                      },
69                                      [](PersistableBundle* bundle, const std::string& key) -> void {
70                                          bundle->putLong(key, nextValue<int64_t>());
71                                      },
72                                      [](PersistableBundle* bundle, const std::string& key) -> void {
73                                          bundle->putDouble(key, nextValue<double>());
74                                      },
75                                      [](PersistableBundle* bundle, const std::string& key) -> void {
76                                          bundle->putString(key, nextValue<std::string>());
77                                      },
78                                      [](PersistableBundle* bundle, const std::string& key) -> void {
79                                          std::vector<bool> value;
80                                          fillVector<bool>(&value);
81                                          bundle->putBooleanVector(key, value);
82                                      },
83                                      [](PersistableBundle* bundle, const std::string& key) -> void {
84                                          std::vector<int32_t> value;
85                                          fillVector<int32_t>(&value);
86                                          bundle->putIntVector(key, value);
87                                      },
88                                      [](PersistableBundle* bundle, const std::string& key) -> void {
89                                          std::vector<int64_t> value;
90                                          fillVector<int64_t>(&value);
91                                          bundle->putLongVector(key, value);
92                                      },
93                                      [](PersistableBundle* bundle, const std::string& key) -> void {
94                                          std::vector<double> value;
95                                          fillVector<double>(&value);
96                                          bundle->putDoubleVector(key, value);
97                                      },
98                                      [](PersistableBundle* bundle, const std::string& key) -> void {
99                                          std::vector<std::string> value;
100                                          fillVector<std::string>(&value);
101                                          bundle->putStringVector(key, value);
102                                      }};
103 
104 }  // namespace
105 
fillBasicData(PersistableBundle * bundle)106 void fillBasicData(PersistableBundle* bundle) {
107     bundle->putBoolean("test_bool", true);
108     bundle->putInt("test_int", 2147483647);
109     bundle->putLong("test_long", 2147483647L);
110     bundle->putDouble("test_double", 1.23);
111     bundle->putString("test_string", "test data");
112     bundle->putBooleanVector("test_bool_vector", {true, false, false});
113     bundle->putIntVector("test_int_vector", {1, 2, 3, 4});
114     bundle->putLongVector("test_long_vector", {100L, 200L, 300L});
115     bundle->putDoubleVector("test_double_vector", {1.1, 2.2});
116     bundle->putStringVector("test_string_vector", {"test", "val"});
117 }
118 
fillRandomData(PersistableBundle * bundle)119 void fillRandomData(PersistableBundle* bundle) {
120     uint8_t entryCount = nextValue<uint8_t>();
121     for (uint8_t i = 0; i < entryCount; i++) {
122         std::string key(nextValue<std::string>());
123         uint8_t setterIdx = nextValue<uint8_t>(sPersistableBundleSetters.size() - 1);
124         sPersistableBundleSetters[setterIdx](bundle, key);
125     }
126 }
127 
128 }  // namespace testing
129 }  // namespace vibrator
130 }  // namespace hardware
131 }  // namespace android
132 }  // namespace aidl
133 
134 #endif  // VIBRATOR_HAL_PERSISTABLE_BUNDLE_UTILS_H
135