aboutsummaryrefslogtreecommitdiff
path: root/runtime_test.cc
blob: 78075afc8318c3b1fb21cb136028850c4724afc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- mode: C++ -*-
//
// Copyright 2022-2023 Google LLC
//
// Licensed under the Apache License v2.0 with LLVM Exceptions (the
// "License"); you may not use this file except in compliance with the
// License.  You may obtain a copy of the License at
//
//     https://llvm.org/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giuliano Procida

#include "runtime.h"

#include <array>
#include <cstddef>
#include <sstream>
#include <string>

#include <catch2/catch.hpp>

namespace Test {

TEST_CASE("empty") {
  std::ostringstream os;
  {
    const stg::Runtime runtime(os, true);
  }
  CHECK(os.str().empty());
}

TEST_CASE("times") {
  const size_t count = 20;
  std::ostringstream os;
  {
    stg::Runtime runtime(os, true);
    const std::array<const stg::Time, count> timers = {
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
      stg::Time(runtime, "name"),
    };
  }
  std::istringstream is(os.str());
  const std::string name = "name:";
  const std::string ms = "ms";
  size_t index = 0;
  double last_time = 0.0;
  while (is && index < count) {
    std::string first;
    double time;
    std::string second;
    is >> first >> time >> second;
    CHECK(first == name);
    CHECK(time > last_time);
    CHECK(second == ms);
    last_time = time;
    ++index;
  }
  CHECK(index == count);
  std::string junk;
  is >> junk;
  CHECK(junk.empty());
  CHECK(is.eof());
}

TEST_CASE("counters") {
  std::ostringstream os;
  {
    stg::Runtime runtime(os, true);
    stg::Counter a(runtime, "a");
    stg::Counter b(runtime, "b");
    stg::Counter c(runtime, "c");
    const stg::Counter d(runtime, "d");
    stg::Counter e(runtime, "e");
    c = 17;
    ++b;
    ++b;
    e = 1;
    a = 3;
    c += 2;
  }
  const std::string expected = "e: 1\nd: 0\nc: 19\nb: 2\na: 3\n";
  CHECK(os.str() == expected);
}

TEST_CASE("histogram") {
  std::ostringstream os;
  {
    stg::Runtime runtime(os, true);
    stg::Histogram h(runtime, "h");
    h.Add(13);
    h.Add(14);
    h.Add(13);
    h.Add(12);
  }
  const std::string expected = "h: [12]=1 [13]=2 [14]=1\n";
  CHECK(os.str() == expected);
}

}  // namespace Test