Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 241a4b24ceb655238e52012576b0d242b1792c39 (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
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/*
 * Created on Jul 19, 2004
 */
package org.eclipse.cdt.core.parser.tests;

import org.eclipse.cdt.core.parser.util.ObjectMap;

import junit.framework.TestCase;

/**
 * Tests for {@link ObjectMap}.
 */
public class ObjectMapTest extends TestCase {

    private static class HashObject {
        final public int hash;

        HashObject(int h) {
            hash = h;
        }

        @Override
		public int hashCode() {
            return hash;
        }
    }

    public void insertContents(ObjectMap map, Object[][] contents) throws Exception {
        for (int i = 0; i < contents.length; i++) {
            map.put(contents[i][0], contents[i][1]);
        }
    }

    public void assertContents(ObjectMap map, Object[][] contents) throws Exception {
        for (int i = 0; i < contents.length; i++) {
            assertEquals(map.keyAt(i), contents[i][0]);
            assertEquals(map.getAt(i), contents[i][1]);
            assertEquals(map.get(contents[i][0]), contents[i][1]);
        }
        assertEquals(map.size(), contents.length);
    }

    public void testSimpleAdd() throws Exception{
        ObjectMap map = new ObjectMap(2);

        Object[][] contents = new Object[][] { {"1", "ob" } };

        insertContents(map, contents);
        assertContents(map, contents);

        assertEquals(map.size(), 1);
        assertEquals(map.capacity(), 8);
    }

    public void testSimpleCollision() throws Exception{
        ObjectMap map = new ObjectMap(2);

        HashObject key1 = new HashObject(1);
        HashObject key2 = new HashObject(1);

        Object[][] contents = new Object[][] { {key1, "1" },
                							   {key2, "2" } };

        insertContents(map, contents);

        assertEquals(map.size(), 2);
        assertEquals(map.capacity(), 8);

        assertContents(map, contents);
    }

    public void testResize() throws Exception{
        ObjectMap map = new ObjectMap(1);

        assertEquals(map.size(), 0);
        assertEquals(map.capacity(), 8);

        Object[][] res = new Object[][] { { "0", "o0" },
							              { "1", "o1" },
							              { "2", "o2" },
							              { "3", "o3" },
							              { "4", "o4" } };

        insertContents(map, res);
        assertEquals(map.capacity(), 8);
        assertContents(map, res);
    }

    public void testCollisionResize() throws Exception{
        ObjectMap map = new ObjectMap(1);

        assertEquals(map.size(), 0);
        assertEquals(map.capacity(), 8);

        Object[][] res = new Object[][] { { new HashObject(0), "o0" },
							              { new HashObject(1), "o1" },
							              { new HashObject(0), "o2" },
							              { new HashObject(1), "o3" },
							              { new HashObject(0), "o4" } };

        insertContents(map, res);
        assertEquals(map.capacity(), 8);
        assertContents(map, res);
    }

    public void testReAdd() throws Exception{
        ObjectMap map = new ObjectMap(1);

        assertEquals(map.size(), 0);
        assertEquals(map.capacity(), 8);

        Object[][] res = new Object[][] { { "0", "o0" },
							              { "1", "o1" } };

        insertContents(map, res);
        assertEquals(map.capacity(), 8);
        assertContents(map, res);

        res = new Object[][]{ { "0",  "o00" },
                			  { "1",  "o01" },
                			  { "10", "o10" },
        					  { "11", "o11" } };

        insertContents(map, res);
        assertContents(map, res);
    }
}

Back to the top