1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_BARD_UNITTEST_UTIL_H_
16 : #define SYZYGY_BARD_UNITTEST_UTIL_H_
17 :
18 : #include <memory>
19 :
20 : #include "gtest/gtest.h"
21 : #include "syzygy/bard/event.h"
22 : #include "syzygy/bard/trace_live_map.h"
23 :
24 : namespace testing {
25 :
26 : using bard::TraceLiveMap;
27 :
28 : template <typename T>
29 : void CheckTraceLiveMapContains(const TraceLiveMap<T>& const_trace_live_map,
30 : T trace,
31 E : T live) {
32 E : TraceLiveMap<T>& trace_live_map =
33 : const_cast<TraceLiveMap<T>&>(const_trace_live_map);
34 :
35 : T answer;
36 E : EXPECT_TRUE(trace_live_map.GetLiveFromTrace(trace, &answer));
37 E : EXPECT_EQ(live, answer);
38 E : EXPECT_TRUE(trace_live_map.GetTraceFromLive(live, &answer));
39 E : EXPECT_EQ(trace, answer);
40 E : }
41 :
42 : template <typename T>
43 : void CheckTraceLiveMapNotContain(const TraceLiveMap<T>& const_trace_live_map,
44 : T trace,
45 E : T live) {
46 E : TraceLiveMap<T>& trace_live_map =
47 : const_cast<TraceLiveMap<T>&>(const_trace_live_map);
48 :
49 : T answer;
50 E : EXPECT_FALSE(trace_live_map.GetLiveFromTrace(trace, &answer));
51 E : EXPECT_FALSE(trace_live_map.GetTraceFromLive(live, &answer));
52 E : }
53 :
54 : template <typename Event>
55 E : void TestEventSerialization(const Event& original_data) {
56 E : core::ByteVector bytes;
57 :
58 E : core::ScopedOutStreamPtr out_stream;
59 E : out_stream.reset(core::CreateByteOutStream(std::back_inserter(bytes)));
60 E : core::NativeBinaryOutArchive out_archive(out_stream.get());
61 E : EXPECT_TRUE(Event::Save(&original_data, &out_archive));
62 E : EXPECT_TRUE(out_archive.Flush());
63 :
64 E : core::ScopedInStreamPtr in_stream;
65 E : in_stream.reset(core::CreateByteInStream(bytes.begin(), bytes.end()));
66 E : core::NativeBinaryInArchive in_archive(in_stream.get());
67 E : std::unique_ptr<Event> data_copy = std::move(Event::Load(&in_archive));
68 E : EXPECT_NE(static_cast<Event*>(nullptr), data_copy.get());
69 :
70 : // Expect equality.
71 E : EXPECT_TRUE(original_data.Equals(data_copy.get()));
72 E : }
73 :
74 : } // namespace testing
75 :
76 : #endif // SYZYGY_BARD_UNITTEST_UTIL_H_
|