aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/cache/CalcTestCase.java
blob: 002b1de9147546c1df87a46bf996e2bc3415d8a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.cache;

import java.util.ArrayList;
import java.util.List;

public class CalcTestCase extends junit.framework.TestCase {

    private SizeCalculator calc;


    public CalcTestCase (String name) {
        super(name);
    }

    public void setUp() {
        calc = new SizeCalculator();
    }

    public void testCalc1() {
        assertEquals(calc.sizeOf(new Object()), 8);
    }

    public void testCalc2() {
        assertEquals(calc.sizeOf(new SixtyFourBooleans()), 8+64);
    }

    public void testBoolean() {
        assertEquals(8+1, calc.sizeOf(new Boolean(true)));
    }

    public void testArrayPrimitive() {
        byte[] eightBytes = new byte[]{1,1,1,1,1,1,1,1,};
        assertEquals(16+8, calc.sizeOf(eightBytes));
    }

    public void testArrayObjects() {
        SixtyFourBooleans[] bunchOfBooleans = new SixtyFourBooleans[]{new SixtyFourBooleans(),
                new SixtyFourBooleans(), new SixtyFourBooleans()};
        assertEquals(16+(3*(8+64)+(3*4)), calc.sizeOf(bunchOfBooleans));

    }

    public void testSizeOfList() {
        SixtyFourBooleans sfb = new SixtyFourBooleans();
        List<Object> dupList1 = new ArrayList<>();
        dupList1.add(new Object());
        dupList1.add(sfb);
        dupList1.add(sfb);
        dupList1.add(sfb);
        List<Object> dupList2 = new ArrayList<>();
        dupList2.addAll(dupList1);
        dupList2.add(sfb);
        dupList2.add(sfb);
        dupList2.add(sfb);
        dupList2.add(new Object());
        dupList2.add(new Object());
        assertEquals(calc.sizeOf(dupList2), calc.sizeOf(dupList1)+8+8);
    }

    public void testSizeOfTuple() {
        SixtyFourBooleans[] bunchOfBooleans = new SixtyFourBooleans[]{new SixtyFourBooleans(),
                new SixtyFourBooleans(), new SixtyFourBooleans()};
        SixtyFourBooleans[] bunchOfBooleans2 = new SixtyFourBooleans[]{new SixtyFourBooleans(),
                new SixtyFourBooleans(), new SixtyFourBooleans()};
        assertEquals(16+(3*(8+64)+(3*4)), calc.sizeOf(bunchOfBooleans));
        assertEquals(2* (16+(3*(8+64)+(3*4))), calc.sizeOf(bunchOfBooleans, bunchOfBooleans2));
    }

    /*public void testEmptyArrayList() {
        assertEquals(80, calc.sizeOf(new ArrayList()));
    }*/

    /*public void testFullArrayList() {
        ArrayList arrayList = new ArrayList(10000);

        for (int i = 0; i < 10000; i++) {
            arrayList.add(new Object());
        }

        assertEquals(120040, calc.sizeOf(arrayList));
    }*/

    /*public void testHashMap() {
        assertEquals(120, calc.sizeOf(new HashMap()));

        Byte[] all = new Byte[256];
        for (int i = -128; i < 128; i++) {
            all[i + 128] = new Byte((byte) i);
        }
        assertEquals(5136, calc.sizeOf(all));

        HashMap hm = new HashMap();
        for (int i = -128; i < 128; i++) {
            hm.put("" + i, new Byte((byte) i));
        }
        assertEquals(30776, calc.sizeOf(hm));
    }*/

    /*public void testThousandBooleansObjects() {
        Boolean[] booleans = new Boolean[1000];

        for (int i = 0; i < booleans.length; i++)
            booleans[i] = new Boolean(true);

        assertEquals(20016, calc.sizeOf(booleans));
    }*/

    @SuppressWarnings("unused")
    private static class SixtyFourBooleans {
        boolean a0;
        boolean a1;
        boolean a2;
        boolean a3;
        boolean a4;
        boolean a5;
        boolean a6;
        boolean a7;
        boolean b0;
        boolean b1;
        boolean b2;
        boolean b3;
        boolean b4;
        boolean b5;
        boolean b6;
        boolean b7;
        boolean c0;
        boolean c1;
        boolean c2;
        boolean c3;
        boolean c4;
        boolean c5;
        boolean c6;
        boolean c7;
        boolean d0;
        boolean d1;
        boolean d2;
        boolean d3;
        boolean d4;
        boolean d5;
        boolean d6;
        boolean d7;
        boolean e0;
        boolean e1;
        boolean e2;
        boolean e3;
        boolean e4;
        boolean e5;
        boolean e6;
        boolean e7;
        boolean f0;
        boolean f1;
        boolean f2;
        boolean f3;
        boolean f4;
        boolean f5;
        boolean f6;
        boolean f7;
        boolean g0;
        boolean g1;
        boolean g2;
        boolean g3;
        boolean g4;
        boolean g5;
        boolean g6;
        boolean g7;
        boolean h0;
        boolean h1;
        boolean h2;
        boolean h3;
        boolean h4;
        boolean h5;
        boolean h6;
        boolean h7;
    }
}