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