1 //===-- ClusteringTest.cpp --------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Clustering.h"
11 #include "BenchmarkResult.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16
17 namespace exegesis {
18
19 namespace {
20
21 using testing::Field;
22 using testing::UnorderedElementsAre;
23 using testing::UnorderedElementsAreArray;
24
TEST(ClusteringTest,Clusters3D)25 TEST(ClusteringTest, Clusters3D) {
26 std::vector<InstructionBenchmark> Points(6);
27
28 // Cluster around (x=0, y=1, z=2): points {0, 3}.
29 Points[0].Measurements = {{"x", 0.01, ""}, {"y", 1.02, ""}, {"z", 1.98, "A"}};
30 Points[3].Measurements = {{"x", -0.01, ""}, {"y", 1.02, ""}, {"z", 1.98, ""}};
31 // Cluster around (x=1, y=1, z=2): points {1, 4}.
32 Points[1].Measurements = {{"x", 1.01, ""}, {"y", 1.02, ""}, {"z", 1.98, ""}};
33 Points[4].Measurements = {{"x", 0.99, ""}, {"y", 1.02, ""}, {"z", 1.98, ""}};
34 // Cluster around (x=0, y=0, z=0): points {5}, marked as noise.
35 Points[5].Measurements = {{"x", 0.0, ""}, {"y", 0.01, ""}, {"z", -0.02, ""}};
36 // Error cluster: points {2}
37 Points[2].Error = "oops";
38
39 auto HasPoints = [](const std::vector<int> &Indices) {
40 return Field(&InstructionBenchmarkClustering::Cluster::PointIndices,
41 UnorderedElementsAreArray(Indices));
42 };
43
44 auto Clustering = InstructionBenchmarkClustering::create(Points, 2, 0.25);
45 ASSERT_TRUE((bool)Clustering);
46 EXPECT_THAT(Clustering.get().getValidClusters(),
47 UnorderedElementsAre(HasPoints({0, 3}), HasPoints({1, 4})));
48 EXPECT_THAT(Clustering.get().getCluster(
49 InstructionBenchmarkClustering::ClusterId::noise()),
50 HasPoints({5}));
51 EXPECT_THAT(Clustering.get().getCluster(
52 InstructionBenchmarkClustering::ClusterId::error()),
53 HasPoints({2}));
54
55 EXPECT_EQ(Clustering.get().getClusterIdForPoint(2),
56 InstructionBenchmarkClustering::ClusterId::error());
57 EXPECT_EQ(Clustering.get().getClusterIdForPoint(5),
58 InstructionBenchmarkClustering::ClusterId::noise());
59 EXPECT_EQ(Clustering.get().getClusterIdForPoint(0),
60 Clustering.get().getClusterIdForPoint(3));
61 EXPECT_EQ(Clustering.get().getClusterIdForPoint(1),
62 Clustering.get().getClusterIdForPoint(4));
63 }
64
TEST(ClusteringTest,Clusters3D_InvalidSize)65 TEST(ClusteringTest, Clusters3D_InvalidSize) {
66 std::vector<InstructionBenchmark> Points(6);
67 Points[0].Measurements = {{"x", 0.01, ""}, {"y", 1.02, ""}, {"z", 1.98, ""}};
68 Points[1].Measurements = {{"y", 1.02, ""}, {"z", 1.98, ""}};
69 auto Error =
70 InstructionBenchmarkClustering::create(Points, 2, 0.25).takeError();
71 ASSERT_TRUE((bool)Error);
72 consumeError(std::move(Error));
73 }
74
TEST(ClusteringTest,Clusters3D_InvalidOrder)75 TEST(ClusteringTest, Clusters3D_InvalidOrder) {
76 std::vector<InstructionBenchmark> Points(6);
77 Points[0].Measurements = {{"x", 0.01, ""}, {"y", 1.02, ""}};
78 Points[1].Measurements = {{"y", 1.02, ""}, {"x", 1.98, ""}};
79 auto Error =
80 InstructionBenchmarkClustering::create(Points, 2, 0.25).takeError();
81 ASSERT_TRUE((bool)Error);
82 consumeError(std::move(Error));
83 }
84
TEST(ClusteringTest,Ordering)85 TEST(ClusteringTest, Ordering) {
86 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::makeValid(1),
87 InstructionBenchmarkClustering::ClusterId::makeValid(2));
88
89 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::makeValid(2),
90 InstructionBenchmarkClustering::ClusterId::noise());
91
92 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::makeValid(2),
93 InstructionBenchmarkClustering::ClusterId::error());
94
95 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::noise(),
96 InstructionBenchmarkClustering::ClusterId::error());
97 }
98
99 } // namespace
100 } // namespace exegesis
101