Jlm
AnnotationMapTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
9 
10 TEST(AnnotationMapTests, AnnotationKeyValueRetrieval)
11 {
12  using namespace jlm::util;
13 
14  // Arrange
15  Annotation stringAnnotation("string", "value");
16  Annotation intAnnotation("int", (int64_t)-1);
17  Annotation uintAnnotation("uint", (uint64_t)1);
18  Annotation doubleAnnotation("double", 1.0);
19 
20  // Act & Assert
21  EXPECT_EQ(stringAnnotation.Label(), "string");
22  EXPECT_EQ(stringAnnotation.Value<std::string>(), "value");
23  EXPECT_TRUE(stringAnnotation.HasValueType<std::string>());
24  EXPECT_FALSE(stringAnnotation.HasValueType<uint64_t>());
25 
26  EXPECT_EQ(intAnnotation.Label(), "int");
27  EXPECT_EQ(intAnnotation.Value<int64_t>(), -1);
28  EXPECT_TRUE(intAnnotation.HasValueType<int64_t>());
29  EXPECT_FALSE(intAnnotation.HasValueType<uint64_t>());
30 
31  EXPECT_EQ(uintAnnotation.Label(), "uint");
32  EXPECT_EQ(uintAnnotation.Value<uint64_t>(), 1u);
33  EXPECT_TRUE(uintAnnotation.HasValueType<uint64_t>());
34 
35  EXPECT_EQ(doubleAnnotation.Label(), "double");
36  EXPECT_EQ(doubleAnnotation.Value<double>(), 1.0);
37  EXPECT_FALSE(doubleAnnotation.HasValueType<uint64_t>());
38 
39  EXPECT_THROW((void)doubleAnnotation.Value<uint64_t>(), std::bad_variant_access);
40 }
41 
42 TEST(AnnotationMapTests, AnnotationEquality)
43 {
44  using namespace jlm::util;
45 
46  // Arrange
47  Annotation stringAnnotation("string", "value");
48  Annotation intAnnotation("int", (int64_t)-1);
49  Annotation uintAnnotation("uint", (uint64_t)1);
50  Annotation doubleAnnotation("double", 1.0);
51 
52  // Act & Assert
53  EXPECT_NE(stringAnnotation, doubleAnnotation);
54  EXPECT_NE(stringAnnotation, intAnnotation);
55  EXPECT_NE(stringAnnotation, uintAnnotation);
56 
57  Annotation otherStringAnnotation("string", "value");
58  EXPECT_EQ(stringAnnotation, otherStringAnnotation);
59 
60  Annotation otherIntAnnotation("uint", (int64_t)1);
61  EXPECT_NE(uintAnnotation, otherIntAnnotation);
62 }
63 
64 TEST(AnnotationMapTests, AnnotationMap)
65 {
66  using namespace jlm::util;
67 
68  // Arrange
69  constexpr size_t key1 = 0;
70  constexpr size_t key2 = 1;
71  Annotation annotation("foo", "bar");
72 
74  map.AddAnnotation(&key1, annotation);
75 
76  // Act & Assert
77  EXPECT_TRUE(map.HasAnnotations(&key1));
78  EXPECT_FALSE(map.HasAnnotations(&key2));
79 
80  auto annotations = map.GetAnnotations(&key1);
81  EXPECT_EQ(annotations.size(), 1u);
82  EXPECT_EQ(annotations[0], annotation);
83 
84  for (auto & iteratedAnnotations : map.Annotations())
85  {
86  for (auto & iteratedAnnotation : iteratedAnnotations)
87  {
88  EXPECT_EQ(iteratedAnnotation, annotation);
89  }
90  }
91 }
TEST(AnnotationMapTests, AnnotationKeyValueRetrieval)
void AddAnnotation(const void *key, Annotation annotation)
const std::vector< Annotation > & GetAnnotations(const void *key) const noexcept
AnnotationRange Annotations() const
bool HasAnnotations(const void *key) const noexcept
bool HasValueType() const noexcept
const TValue & Value() const
const std::string_view & Label() const noexcept