1 /* 2 * Copyright (C) 2019 The Android Open Source Project 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.android.server.wifi.util; 18 19 import static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertThat; 21 22 import androidx.test.filters.SmallTest; 23 24 import com.android.server.wifi.WifiBaseTest; 25 import com.android.server.wifi.util.ObjectCounter.ProtobufConverter; 26 27 import org.hamcrest.collection.IsIterableContainingInAnyOrder; 28 import org.junit.Test; 29 30 import java.util.Arrays; 31 import java.util.Objects; 32 33 34 /** 35 * Unit test for ObjectCounter 36 */ 37 @SmallTest 38 public class ObjectCounterTest extends WifiBaseTest { 39 40 /** 41 * Test Key type: composite key with 3 fields 42 */ 43 public static class TestKey { 44 public int key1; 45 public String key2; 46 public boolean key3; 47 TestKey(int key1, String key2, boolean key3)48 public TestKey(int key1, String key2, boolean key3) { 49 this.key1 = key1; 50 this.key2 = key2; 51 this.key3 = key3; 52 } 53 54 // generated by IntelliJ 55 @Override equals(Object o)56 public boolean equals(Object o) { 57 if (this == o) return true; 58 if (o == null || getClass() != o.getClass()) return false; 59 TestKey testKey = (TestKey) o; 60 return key1 == testKey.key1 61 && key3 == testKey.key3 62 && Objects.equals(key2, testKey.key2); 63 } 64 65 // generated by IntelliJ 66 @Override hashCode()67 public int hashCode() { 68 return Objects.hash(key1, key2, key3); 69 } 70 71 // generated by IntelliJ 72 @Override toString()73 public String toString() { 74 return "TestKey{" 75 + "key1=" + key1 76 + ", key2='" + key2 + '\'' 77 + ", key3=" + key3 78 + '}'; 79 } 80 } 81 82 /** 83 * Test Proto type with 3 keys and a count field 84 */ 85 public static class TestKeyProto { 86 public int key1; 87 public String key2; 88 public boolean key3; 89 public int count; 90 TestKeyProto(int key1, String key2, boolean key3, int count)91 public TestKeyProto(int key1, String key2, boolean key3, int count) { 92 this.key1 = key1; 93 this.key2 = key2; 94 this.key3 = key3; 95 this.count = count; 96 } 97 98 @Override equals(Object o)99 public boolean equals(Object o) { 100 if (this == o) return true; 101 if (o == null || getClass() != o.getClass()) return false; 102 TestKeyProto that = (TestKeyProto) o; 103 return key1 == that.key1 104 && key3 == that.key3 105 && count == that.count 106 && Objects.equals(key2, that.key2); 107 } 108 109 @Override hashCode()110 public int hashCode() { 111 return Objects.hash(key1, key2, key3, count); 112 } 113 } 114 115 private static final TestKey[] TEST_KEYS = { 116 new TestKey(10, "asdf", false), 117 new TestKey(5, "qwer", true), 118 new TestKey(10, "asdf", false), 119 new TestKey(10, "asdf", false), 120 new TestKey(-1, "zxcv", true), 121 new TestKey(-1, "zxcv", true), 122 new TestKey(-1, "zxcv", true), 123 }; 124 125 126 private static final ProtobufConverter<TestKey, TestKeyProto> CONVERTER = 127 (TestKey key, int count) -> new TestKeyProto(key.key1, key.key2, key.key3, count); 128 129 /** 130 * Tests when the counter is empty. 131 */ 132 @Test testEmpty()133 public void testEmpty() { 134 assertArrayEquals(new TestKeyProto[0], 135 new ObjectCounter<TestKey>().toProto(TestKeyProto.class, CONVERTER)); 136 } 137 138 /** 139 * Tests adding to the counter. 140 */ 141 @Test testAddToCounter()142 public void testAddToCounter() { 143 ObjectCounter<TestKey> counter = new ObjectCounter<>(); 144 for (TestKey key : TEST_KEYS) { 145 counter.increment(key); 146 } 147 148 TestKeyProto[] expected = { 149 new TestKeyProto(10, "asdf", false, 3), 150 new TestKeyProto(5, "qwer", true, 1), 151 new TestKeyProto(-1, "zxcv", true, 3) 152 }; 153 154 TestKeyProto[] actual = counter.toProto(TestKeyProto.class, CONVERTER); 155 156 assertThat("Unexpected output protobuf array (ignoring order)", 157 Arrays.asList(actual), IsIterableContainingInAnyOrder.containsInAnyOrder(expected)); 158 } 159 } 160