• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google Inc.
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  */
16 
17 package com.google.caliper.memory;
18 
19 import com.google.caliper.memory.ObjectGraphMeasurer.Footprint;
20 import com.google.common.collect.ImmutableMultiset;
21 
22 import junit.framework.TestCase;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 /**
29  * Tests for ObjectGraphMeasurer.
30  */
31 @RunWith(JUnit4.class)
32 public class ObjectGraphMeasurerTest extends TestCase {
33   enum DummyEnum {
34     VALUE;
35   }
36   static final Object oneEnumField = new Object() {
37     @SuppressWarnings("unused") DummyEnum enumField = DummyEnum.VALUE;
38   };
39 
40   // enums are treated as statics (and ignored)
testEnum()41   @Test public void testEnum() {
42     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(oneEnumField);
43     assertEquals(new Footprint(1, 1, 0, NO_PRIMITIVES), footprint);
44   }
45 
46   static final Object oneClassField = new Object() {
47     @SuppressWarnings("unused") Class<?> clazz = Object.class;
48   };
49 
50   // Class instances are treated as statics (and ignored)
testClass()51   @Test public void testClass() {
52     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(oneClassField);
53     assertEquals(new ObjectGraphMeasurer.Footprint(1, 1, 0, NO_PRIMITIVES), footprint);
54   }
55 
56   static final Object oneObjectField = new Object() {
57     @SuppressWarnings("unused") Object objectField = new Object();
58   };
59 
testObject()60   @Test public void testObject() {
61     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(oneObjectField);
62     assertEquals(new ObjectGraphMeasurer.Footprint(2, 1, 0, NO_PRIMITIVES), footprint);
63   }
64 
65   static final Object withCycle = new Object() {
66     Object[] array = new Object[1];
67     {
68       array[0] = this;
69     }
70   };
71 
testCycle()72   @Test public void testCycle() {
73     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(withCycle);
74     assertEquals(new ObjectGraphMeasurer.Footprint(2, 2, 0, NO_PRIMITIVES), footprint);
75   }
76 
77   static final Object multiplePathsToObject = new Object() {
78     Object object = new Object();
79     @SuppressWarnings("unused") Object ref1 = object;
80     @SuppressWarnings("unused") Object ref2 = object;
81   };
82 
testMultiplePathsToObject()83   @Test public void testMultiplePathsToObject() {
84     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(multiplePathsToObject);
85     assertEquals(new ObjectGraphMeasurer.Footprint(2, 3, 0, NO_PRIMITIVES), footprint);
86   }
87 
88   static final Object multiplePathsToClass = new Object() {
89     Object object = Object.class;
90     @SuppressWarnings("unused") Object ref1 = object;
91     @SuppressWarnings("unused") Object ref2 = object;
92   };
93 
testMultiplePathsToClass()94   @Test public void testMultiplePathsToClass() {
95     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(multiplePathsToClass);
96     assertEquals(new ObjectGraphMeasurer.Footprint(1, 3, 0, NO_PRIMITIVES), footprint);
97   }
98 
99   static class WithStaticField {
100     static WithStaticField INSTANCE = new WithStaticField();
101   }
102 
testStaticFields()103   @Test public void testStaticFields() {
104     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(new WithStaticField());
105     assertEquals(new ObjectGraphMeasurer.Footprint(1, 0, 0, NO_PRIMITIVES), footprint);
106   }
107 
108   @SuppressWarnings("unused") // unused test fields
109   static final Object oneNullOneNonNull = new Object() {
110     Object nonNull1 = new Object();
111     Object nonNull2 = nonNull1;
112     Object null1 = null;
113     Object null2 = null;
114     Object null3 = null;
115   };
116 
testNullField()117   @Test public void testNullField() {
118     ObjectGraphMeasurer.Footprint footprint = ObjectGraphMeasurer.measure(oneNullOneNonNull);
119     assertEquals(new ObjectGraphMeasurer.Footprint(2, 2, 3, NO_PRIMITIVES), footprint);
120   }
121 
122   private static final ImmutableMultiset<Class<?>> NO_PRIMITIVES = ImmutableMultiset.of();
123 }
124