1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*- 3*9e3b08aeSAndroid Build Coastguard Worker // 4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2022-2023 Google LLC 5*9e3b08aeSAndroid Build Coastguard Worker // 6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the 7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the 8*9e3b08aeSAndroid Build Coastguard Worker // License. You may obtain a copy of the License at 9*9e3b08aeSAndroid Build Coastguard Worker // 10*9e3b08aeSAndroid Build Coastguard Worker // https://llvm.org/LICENSE.txt 11*9e3b08aeSAndroid Build Coastguard Worker // 12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License. 17*9e3b08aeSAndroid Build Coastguard Worker // 18*9e3b08aeSAndroid Build Coastguard Worker // Author: Giuliano Procida 19*9e3b08aeSAndroid Build Coastguard Worker 20*9e3b08aeSAndroid Build Coastguard Worker #include "runtime.h" 21*9e3b08aeSAndroid Build Coastguard Worker 22*9e3b08aeSAndroid Build Coastguard Worker #include <array> 23*9e3b08aeSAndroid Build Coastguard Worker #include <cstddef> 24*9e3b08aeSAndroid Build Coastguard Worker #include <sstream> 25*9e3b08aeSAndroid Build Coastguard Worker #include <string> 26*9e3b08aeSAndroid Build Coastguard Worker 27*9e3b08aeSAndroid Build Coastguard Worker #include <catch2/catch.hpp> 28*9e3b08aeSAndroid Build Coastguard Worker 29*9e3b08aeSAndroid Build Coastguard Worker namespace Test { 30*9e3b08aeSAndroid Build Coastguard Worker 31*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("empty") { 32*9e3b08aeSAndroid Build Coastguard Worker std::ostringstream os; 33*9e3b08aeSAndroid Build Coastguard Worker { 34*9e3b08aeSAndroid Build Coastguard Worker const stg::Runtime runtime(os, true); 35*9e3b08aeSAndroid Build Coastguard Worker } 36*9e3b08aeSAndroid Build Coastguard Worker CHECK(os.str().empty()); 37*9e3b08aeSAndroid Build Coastguard Worker } 38*9e3b08aeSAndroid Build Coastguard Worker 39*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("times") { 40*9e3b08aeSAndroid Build Coastguard Worker const size_t count = 20; 41*9e3b08aeSAndroid Build Coastguard Worker std::ostringstream os; 42*9e3b08aeSAndroid Build Coastguard Worker { 43*9e3b08aeSAndroid Build Coastguard Worker stg::Runtime runtime(os, true); 44*9e3b08aeSAndroid Build Coastguard Worker const std::array<const stg::Time, count> timers = { 45*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 46*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 47*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 48*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 49*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 50*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 51*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 52*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 53*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 54*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 55*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 56*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 57*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 58*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 59*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 60*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 61*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 62*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 63*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 64*9e3b08aeSAndroid Build Coastguard Worker stg::Time(runtime, "name"), 65*9e3b08aeSAndroid Build Coastguard Worker }; 66*9e3b08aeSAndroid Build Coastguard Worker } 67*9e3b08aeSAndroid Build Coastguard Worker std::istringstream is(os.str()); 68*9e3b08aeSAndroid Build Coastguard Worker const std::string name = "name:"; 69*9e3b08aeSAndroid Build Coastguard Worker const std::string ms = "ms"; 70*9e3b08aeSAndroid Build Coastguard Worker size_t index = 0; 71*9e3b08aeSAndroid Build Coastguard Worker double last_time = 0.0; 72*9e3b08aeSAndroid Build Coastguard Worker while (is && index < count) { 73*9e3b08aeSAndroid Build Coastguard Worker std::string first; 74*9e3b08aeSAndroid Build Coastguard Worker double time; 75*9e3b08aeSAndroid Build Coastguard Worker std::string second; 76*9e3b08aeSAndroid Build Coastguard Worker is >> first >> time >> second; 77*9e3b08aeSAndroid Build Coastguard Worker CHECK(first == name); 78*9e3b08aeSAndroid Build Coastguard Worker CHECK(time > last_time); 79*9e3b08aeSAndroid Build Coastguard Worker CHECK(second == ms); 80*9e3b08aeSAndroid Build Coastguard Worker last_time = time; 81*9e3b08aeSAndroid Build Coastguard Worker ++index; 82*9e3b08aeSAndroid Build Coastguard Worker } 83*9e3b08aeSAndroid Build Coastguard Worker CHECK(index == count); 84*9e3b08aeSAndroid Build Coastguard Worker std::string junk; 85*9e3b08aeSAndroid Build Coastguard Worker is >> junk; 86*9e3b08aeSAndroid Build Coastguard Worker CHECK(junk.empty()); 87*9e3b08aeSAndroid Build Coastguard Worker CHECK(is.eof()); 88*9e3b08aeSAndroid Build Coastguard Worker } 89*9e3b08aeSAndroid Build Coastguard Worker 90*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("counters") { 91*9e3b08aeSAndroid Build Coastguard Worker std::ostringstream os; 92*9e3b08aeSAndroid Build Coastguard Worker { 93*9e3b08aeSAndroid Build Coastguard Worker stg::Runtime runtime(os, true); 94*9e3b08aeSAndroid Build Coastguard Worker stg::Counter a(runtime, "a"); 95*9e3b08aeSAndroid Build Coastguard Worker stg::Counter b(runtime, "b"); 96*9e3b08aeSAndroid Build Coastguard Worker stg::Counter c(runtime, "c"); 97*9e3b08aeSAndroid Build Coastguard Worker const stg::Counter d(runtime, "d"); 98*9e3b08aeSAndroid Build Coastguard Worker stg::Counter e(runtime, "e"); 99*9e3b08aeSAndroid Build Coastguard Worker c = 17; 100*9e3b08aeSAndroid Build Coastguard Worker ++b; 101*9e3b08aeSAndroid Build Coastguard Worker ++b; 102*9e3b08aeSAndroid Build Coastguard Worker e = 1; 103*9e3b08aeSAndroid Build Coastguard Worker a = 3; 104*9e3b08aeSAndroid Build Coastguard Worker c += 2; 105*9e3b08aeSAndroid Build Coastguard Worker } 106*9e3b08aeSAndroid Build Coastguard Worker const std::string expected = "e: 1\nd: 0\nc: 19\nb: 2\na: 3\n"; 107*9e3b08aeSAndroid Build Coastguard Worker CHECK(os.str() == expected); 108*9e3b08aeSAndroid Build Coastguard Worker } 109*9e3b08aeSAndroid Build Coastguard Worker 110*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("histogram") { 111*9e3b08aeSAndroid Build Coastguard Worker std::ostringstream os; 112*9e3b08aeSAndroid Build Coastguard Worker { 113*9e3b08aeSAndroid Build Coastguard Worker stg::Runtime runtime(os, true); 114*9e3b08aeSAndroid Build Coastguard Worker stg::Histogram h(runtime, "h"); 115*9e3b08aeSAndroid Build Coastguard Worker h.Add(13); 116*9e3b08aeSAndroid Build Coastguard Worker h.Add(14); 117*9e3b08aeSAndroid Build Coastguard Worker h.Add(13); 118*9e3b08aeSAndroid Build Coastguard Worker h.Add(12); 119*9e3b08aeSAndroid Build Coastguard Worker } 120*9e3b08aeSAndroid Build Coastguard Worker const std::string expected = "h: [12]=1 [13]=2 [14]=1\n"; 121*9e3b08aeSAndroid Build Coastguard Worker CHECK(os.str() == expected); 122*9e3b08aeSAndroid Build Coastguard Worker } 123*9e3b08aeSAndroid Build Coastguard Worker 124*9e3b08aeSAndroid Build Coastguard Worker } // namespace Test 125