1 /* 2 * Copyright (C) 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 #ifndef LIBTEXTCLASSIFIER_UTILS_TESTING_TEST_DATA_GENERATOR_H_ 18 #define LIBTEXTCLASSIFIER_UTILS_TESTING_TEST_DATA_GENERATOR_H_ 19 20 #include <algorithm> 21 #include <iostream> 22 #include <random> 23 #include <string> 24 25 #include "utils/strings/stringpiece.h" 26 27 // Generates test data randomly. 28 class TestDataGenerator { 29 public: TestDataGenerator()30 explicit TestDataGenerator() : random_engine_(0) {} 31 32 template <typename T, 33 typename std::enable_if_t<std::is_integral<T>::value>* = nullptr> generate()34 T generate() { 35 std::uniform_int_distribution<T> dist; 36 return dist(random_engine_); 37 } 38 39 template <> generate()40 bool generate() { 41 std::bernoulli_distribution dist(0.5); 42 return dist(random_engine_); 43 } 44 45 template <> generate()46 char generate() { 47 std::uniform_int_distribution<int> dist(0, 25); 48 return dist(random_engine_) + 'a'; 49 } 50 51 template <typename T, typename std::enable_if_t< 52 std::is_floating_point<T>::value>* = nullptr> generate()53 T generate() { 54 std::uniform_real_distribution<T> dist; 55 return dist(random_engine_); 56 } 57 58 template <typename T, typename std::enable_if_t< 59 std::is_same<std::string, T>::value>* = nullptr> generate()60 T generate() { 61 std::uniform_int_distribution<> dist(1, 10); 62 return std::string(dist(random_engine_), '*'); 63 } 64 65 private: 66 std::default_random_engine random_engine_; 67 }; 68 69 #endif // LIBTEXTCLASSIFIER_UTILS_TESTING_TEST_DATA_GENERATOR_H_ 70