blob: 8f42b0c9a467d52db26fecc10dec69dc48f1afbb [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro12eb78e2011-06-24 14:51:06 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_instruction_visitor.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070018
19#include <iostream>
Elliott Hughes90a33692011-08-30 13:27:07 -070020
21#include "UniquePtr.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070022#include "gtest/gtest.h"
23
24namespace art {
25
26class TestVisitor : public DexInstructionVisitor<TestVisitor> {};
27
Brian Carlstroma331b3c2011-07-18 17:47:56 -070028TEST(InstructionTest, Init) {
Elliott Hughes90a33692011-08-30 13:27:07 -070029 UniquePtr<TestVisitor> visitor(new TestVisitor);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070030}
31
32class CountVisitor : public DexInstructionVisitor<CountVisitor> {
33 public:
34 int count_;
35
36 CountVisitor() : count_(0) {}
37
Elliott Hughes317eb642012-03-16 14:19:10 -070038 void Do_Default(const Instruction*) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070039 ++count_;
40 }
41};
42
Brian Carlstroma331b3c2011-07-18 17:47:56 -070043TEST(InstructionTest, Count) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070044 CountVisitor v0;
jeffhaoba5ebb92011-08-25 17:24:37 -070045 const uint16_t c0[] = {};
Carl Shapiro12eb78e2011-06-24 14:51:06 -070046 v0.Visit(c0, sizeof(c0));
47 EXPECT_EQ(0, v0.count_);
48
49 CountVisitor v1;
jeffhaoba5ebb92011-08-25 17:24:37 -070050 const uint16_t c1[] = { 0 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070051 v1.Visit(c1, sizeof(c1));
52 EXPECT_EQ(1, v1.count_);
53
54 CountVisitor v2;
jeffhaoba5ebb92011-08-25 17:24:37 -070055 const uint16_t c2[] = { 0, 0 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070056 v2.Visit(c2, sizeof(c2));
57 EXPECT_EQ(2, v2.count_);
58
59 CountVisitor v3;
jeffhaoba5ebb92011-08-25 17:24:37 -070060 const uint16_t c3[] = { 0, 0, 0, };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070061 v3.Visit(c3, sizeof(c3));
62 EXPECT_EQ(3, v3.count_);
63
64 CountVisitor v4;
jeffhaoba5ebb92011-08-25 17:24:37 -070065 const uint16_t c4[] = { 0, 0, 0, 0 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070066 v4.Visit(c4, sizeof(c4));
67 EXPECT_EQ(4, v4.count_);
68}
69
70} // namespace art