| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. | 
|  | 2 |  | 
| Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "dex_instruction_visitor.h" | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 4 |  | 
|  | 5 | #include <iostream> | 
| Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 6 |  | 
|  | 7 | #include "UniquePtr.h" | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 8 | #include "gtest/gtest.h" | 
|  | 9 |  | 
|  | 10 | namespace art { | 
|  | 11 |  | 
|  | 12 | class TestVisitor : public DexInstructionVisitor<TestVisitor> {}; | 
|  | 13 |  | 
| Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 14 | TEST(InstructionTest, Init) { | 
| Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 15 | UniquePtr<TestVisitor> visitor(new TestVisitor); | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 16 | } | 
|  | 17 |  | 
|  | 18 | class CountVisitor : public DexInstructionVisitor<CountVisitor> { | 
|  | 19 | public: | 
|  | 20 | int count_; | 
|  | 21 |  | 
|  | 22 | CountVisitor() : count_(0) {} | 
|  | 23 |  | 
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 24 | void Do_Default(const Instruction* inst) { | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 25 | ++count_; | 
|  | 26 | } | 
|  | 27 | }; | 
|  | 28 |  | 
| Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 29 | TEST(InstructionTest, Count) { | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 30 | CountVisitor v0; | 
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 31 | const uint16_t c0[] = {}; | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 32 | v0.Visit(c0, sizeof(c0)); | 
|  | 33 | EXPECT_EQ(0, v0.count_); | 
|  | 34 |  | 
|  | 35 | CountVisitor v1; | 
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 36 | const uint16_t c1[] = { 0 }; | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 37 | v1.Visit(c1, sizeof(c1)); | 
|  | 38 | EXPECT_EQ(1, v1.count_); | 
|  | 39 |  | 
|  | 40 | CountVisitor v2; | 
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 41 | const uint16_t c2[] = { 0, 0 }; | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 42 | v2.Visit(c2, sizeof(c2)); | 
|  | 43 | EXPECT_EQ(2, v2.count_); | 
|  | 44 |  | 
|  | 45 | CountVisitor v3; | 
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 46 | const uint16_t c3[] = { 0, 0, 0, }; | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 47 | v3.Visit(c3, sizeof(c3)); | 
|  | 48 | EXPECT_EQ(3, v3.count_); | 
|  | 49 |  | 
|  | 50 | CountVisitor v4; | 
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 51 | const uint16_t c4[] = { 0, 0, 0, 0  }; | 
| Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 52 | v4.Visit(c4, sizeof(c4)); | 
|  | 53 | EXPECT_EQ(4, v4.count_); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | }  // namespace art |