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