blob: d6eeb66ab3ffddf90aa67b958e73d8c6fec9c9f2 [file] [log] [blame]
Madan Jampani83c27832015-12-07 10:42:13 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onlab.util;
17
18import java.util.Objects;
19
20import org.junit.Test;
21
22import com.google.common.collect.Maps;
23
24import static org.junit.Assert.*;
25
26/**
27 * Unit tests for ExtendedSet.
28 */
29public class ExtendedSetTest {
30
31 @Test
32 public void testGet() {
33 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
34 TestValue e1 = new TestValue("foo", 1);
35 set.add(e1);
36 TestValue lookupValue = new TestValue("foo", 2);
37 TestValue setEntry = set.get(lookupValue);
38 assertEquals(e1, setEntry);
39 }
40
41 @Test
42 public void testInsertOrReplace() {
43 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
44 TestValue small = new TestValue("foo", 1);
45 TestValue medium = new TestValue("foo", 2);
46 TestValue large = new TestValue("foo", 3);
47 // input TestValue will replace existing TestValue if its value2() is greater
48 // than existing entry's value2()
49 assertTrue(set.insertOrReplace(small, existing -> existing.value2() < small.value2()));
50 assertTrue(set.insertOrReplace(large, existing -> existing.value2() < large.value2()));
51 assertFalse(set.insertOrReplace(medium, existing -> existing.value2() < medium.value2()));
52
53 assertTrue(set.contains(small));
54 assertTrue(set.contains(medium));
55 assertTrue(set.contains(large));
56 }
57
58 @Test
59 public void testConditionalRemove() {
60 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
61 TestValue small = new TestValue("foo", 1);
62 TestValue medium = new TestValue("foo", 2);
63
64 assertTrue(set.add(small));
65 set.conditionalRemove(medium, existing -> existing.value2() < medium.value2);
66 assertFalse(set.contains(small));
67 }
68
69 private class TestValue {
70 private String value1;
71 private int value2;
72
73 public TestValue(String v1, int v2) {
74 this.value1 = v1;
75 this.value2 = v2;
76 }
77
78 public String value1() {
79 return value1;
80 }
81
82 public int value2() {
83 return value2;
84 }
85
86 @Override
87 public boolean equals(Object other) {
88 if (other instanceof TestValue) {
89 TestValue that = (TestValue) other;
90 return Objects.equals(value1, that.value1);
91 }
92 return false;
93 }
94
95 @Override
96 public int hashCode() {
97 return Objects.hash(value1);
98 }
99 }
100}