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/grinder/profile_grinder.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/core/unittest_util.h"
19 : #include "syzygy/pe/unittest_util.h"
20 :
21 : namespace grinder {
22 :
23 : namespace {
24 :
25 : const wchar_t kProfileTraceFile[] = L"profile_traces/trace-1.bin";
26 :
27 : class TestProfileGrinder : public ProfileGrinder {
28 : public:
29 : using ProfileGrinder::parser_;
30 : };
31 :
32 : class ProfileGrinderTest : public testing::PELibUnitTest {
33 : public:
34 : typedef testing::PELibUnitTest Super;
35 :
36 E : ProfileGrinderTest() : cmd_line_(FilePath(L"profile_grinder.exe")) {
37 E : }
38 :
39 E : virtual void SetUp() OVERRIDE {
40 E : Super::Test::SetUp();
41 E : cmd_line_.AppendSwitchASCII("mode", "profile");
42 E : }
43 :
44 E : void InitParser(trace::parser::ParseEventHandlerImpl* handler) {
45 E : ASSERT_TRUE(handler != NULL);
46 :
47 E : ASSERT_TRUE(parser_.Init(handler));
48 :
49 : FilePath trace_file =
50 E : testing::GetExeTestDataRelativePath(kProfileTraceFile);
51 :
52 E : ASSERT_TRUE(parser_.OpenTraceFile(trace_file));
53 E : }
54 :
55 E : void GrindAndOutputSucceeds() {
56 E : TestProfileGrinder grinder;
57 E : grinder.ParseCommandLine(&cmd_line_);
58 :
59 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
60 E : grinder.SetParser(&parser_);
61 E : ASSERT_TRUE(parser_.Consume());
62 :
63 E : EXPECT_TRUE(grinder.Grind());
64 :
65 E : testing::ScopedTempFile output_path;
66 : file_util::ScopedFILE output_file(
67 E : file_util::OpenFile(output_path.path(), "wb"));
68 E : ASSERT_TRUE(output_file.get() != NULL);
69 :
70 E : EXPECT_TRUE(grinder.OutputData(output_file.get()));
71 E : output_file.reset();
72 :
73 E : int64 cache_grind_file_size = 0;
74 E : ASSERT_TRUE(file_util::GetFileSize(output_path.path(),
75 : &cache_grind_file_size));
76 E : EXPECT_LT(0u, cache_grind_file_size);
77 E : }
78 :
79 : CommandLine cmd_line_;
80 : trace::parser::Parser parser_;
81 : };
82 :
83 : } // namespace
84 :
85 E : TEST_F(ProfileGrinderTest, ParseEmptyCommandLineSucceeds) {
86 E : TestProfileGrinder grinder;
87 E : EXPECT_TRUE(grinder.ParseCommandLine(&cmd_line_));
88 E : EXPECT_FALSE(grinder.thread_parts());
89 E : }
90 :
91 E : TEST_F(ProfileGrinderTest, ParseThreadPartsSwitchOnCommandLine) {
92 E : TestProfileGrinder grinder;
93 E : cmd_line_.AppendSwitch("thread-parts");
94 E : EXPECT_TRUE(grinder.ParseCommandLine(&cmd_line_));
95 E : }
96 :
97 E : TEST_F(ProfileGrinderTest, SetParserSucceeds) {
98 E : TestProfileGrinder grinder;
99 E : grinder.ParseCommandLine(&cmd_line_);
100 :
101 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
102 :
103 E : grinder.SetParser(&parser_);
104 E : EXPECT_EQ(&parser_, grinder.parser_);
105 E : }
106 :
107 E : TEST_F(ProfileGrinderTest, GrindAndOutputCacheGrindDataSucceeds) {
108 E : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds());
109 : // TODO(etienneb): Validate the output is a valid CacheGrind file.
110 E : }
111 :
112 : } // namespace grinder
|