1 : // Copyright 2012 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 : #include "syzygy/msf/msf_file.h"
16 :
17 : #include <memory>
18 :
19 : #include "gmock/gmock.h"
20 : #include "gtest/gtest.h"
21 :
22 : namespace msf {
23 :
24 : namespace {
25 :
26 : class DummyMsfStream : public MsfStream {
27 : public:
28 E : DummyMsfStream() : MsfStream(10) { ++instance_count_; }
29 :
30 i : bool ReadBytesAt(size_t pos, size_t count, void* dest) override {
31 i : return false;
32 i : }
33 :
34 E : static size_t instance_count() { return instance_count_; }
35 :
36 : protected:
37 E : virtual ~DummyMsfStream() { --instance_count_; }
38 :
39 : static size_t instance_count_;
40 : };
41 :
42 : size_t DummyMsfStream::instance_count_;
43 :
44 : } // namespace
45 :
46 E : TEST(MsfFileTest, Clear) {
47 E : MsfFile msf_file;
48 E : EXPECT_EQ(0u, msf_file.StreamCount());
49 E : EXPECT_EQ(0u, DummyMsfStream::instance_count());
50 :
51 E : msf_file.AppendStream(new DummyMsfStream());
52 E : EXPECT_EQ(1u, msf_file.StreamCount());
53 E : EXPECT_EQ(1u, DummyMsfStream::instance_count());
54 :
55 E : msf_file.AppendStream(new DummyMsfStream());
56 E : EXPECT_EQ(2u, msf_file.StreamCount());
57 E : EXPECT_EQ(2u, DummyMsfStream::instance_count());
58 :
59 E : msf_file.SetStream(100, new DummyMsfStream());
60 E : EXPECT_EQ(101u, msf_file.StreamCount());
61 E : EXPECT_TRUE(msf_file.GetStream(99) == NULL);
62 E : EXPECT_EQ(3u, DummyMsfStream::instance_count());
63 :
64 E : msf_file.Clear();
65 E : EXPECT_EQ(0u, msf_file.StreamCount());
66 E : EXPECT_EQ(0u, DummyMsfStream::instance_count());
67 E : }
68 :
69 E : TEST(MsfFileTest, WorksAsExpected) {
70 E : std::unique_ptr<MsfFile> msf(new MsfFile());
71 E : EXPECT_EQ(0u, msf->StreamCount());
72 E : EXPECT_EQ(0u, DummyMsfStream::instance_count());
73 :
74 E : scoped_refptr<MsfStream> stream(new DummyMsfStream());
75 E : EXPECT_EQ(1u, DummyMsfStream::instance_count());
76 E : size_t index0 = msf->AppendStream(stream.get());
77 E : EXPECT_EQ(0u, index0);
78 E : EXPECT_EQ(1u, msf->StreamCount());
79 E : EXPECT_EQ(stream.get(), msf->GetStream(index0));
80 :
81 E : stream = new DummyMsfStream();
82 E : EXPECT_EQ(2u, DummyMsfStream::instance_count());
83 E : size_t index1 = msf->AppendStream(stream.get());
84 E : EXPECT_EQ(1u, index1);
85 E : EXPECT_EQ(2u, msf->StreamCount());
86 E : EXPECT_EQ(stream.get(), msf->GetStream(index1));
87 E : MsfStream* stream1 = stream.get();
88 :
89 E : stream = new DummyMsfStream();
90 E : EXPECT_EQ(3u, DummyMsfStream::instance_count());
91 E : msf->ReplaceStream(index0, stream.get());
92 E : EXPECT_EQ(2u, DummyMsfStream::instance_count());
93 E : EXPECT_EQ(2u, msf->StreamCount());
94 E : EXPECT_EQ(stream.get(), msf->GetStream(index0));
95 E : MsfStream* stream0 = stream.get();
96 E : stream = NULL;
97 :
98 E : EXPECT_EQ(stream0, msf->GetStream(0));
99 E : EXPECT_EQ(stream1, msf->GetStream(1));
100 :
101 E : msf.reset(NULL);
102 E : EXPECT_EQ(0u, DummyMsfStream::instance_count());
103 E : }
104 :
105 : } // namespace msf
|