|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, 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 | +package rx.functions; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +import java.lang.reflect.*; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.concurrent.atomic.AtomicLong; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +public class ActionsTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testEmptyArities() { |
| 30 | + Action0 a0 = Actions.empty(); |
| 31 | + a0.call(); |
| 32 | + |
| 33 | + Action1<Integer> a1 = Actions.empty(); |
| 34 | + a1.call(1); |
| 35 | + |
| 36 | + Action2<Integer, Integer> a2 = Actions.empty(); |
| 37 | + a2.call(1, 2); |
| 38 | + |
| 39 | + Action3<Integer, Integer, Integer> a3 = Actions.empty(); |
| 40 | + a3.call(1, 2, 3); |
| 41 | + |
| 42 | + Action4<Integer, Integer, Integer, Integer> a4 = Actions.empty(); |
| 43 | + a4.call(1, 2, 3, 4); |
| 44 | + |
| 45 | + Action5<Integer, Integer, Integer, Integer, Integer> a5 = Actions.empty(); |
| 46 | + a5.call(1, 2, 3, 4, 5); |
| 47 | + |
| 48 | + Action6<Integer, Integer, Integer, Integer, Integer, Integer> a6 = Actions.empty(); |
| 49 | + a6.call(1, 2, 3, 4, 5, 6); |
| 50 | + |
| 51 | + Action7<Integer, Integer, Integer, Integer, Integer, Integer, Integer> a7 = Actions.empty(); |
| 52 | + a7.call(1, 2, 3, 4, 5, 6, 7); |
| 53 | + |
| 54 | + Action8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> a8 = Actions.empty(); |
| 55 | + a8.call(1, 2, 3, 4, 5, 6, 7, 8); |
| 56 | + |
| 57 | + Action9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> a9 = Actions.empty(); |
| 58 | + a9.call(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 59 | + |
| 60 | + ActionN an0 = Actions.empty(); |
| 61 | + an0.call(); |
| 62 | + |
| 63 | + ActionN an1 = Actions.empty(); |
| 64 | + an1.call(1); |
| 65 | + |
| 66 | + ActionN ann = Actions.empty(); |
| 67 | + ann.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 68 | + |
| 69 | + ActionN annn = Actions.empty(); |
| 70 | + annn.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 71 | + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testToFunc0() { |
| 76 | + final AtomicLong value = new AtomicLong(-1L); |
| 77 | + final Action0 action = new Action0() { |
| 78 | + @Override |
| 79 | + public void call() { |
| 80 | + value.set(0); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + assertNull(Actions.toFunc(action).call()); |
| 85 | + assertEquals(0, value.get()); |
| 86 | + value.set(-1L); |
| 87 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call()); |
| 88 | + assertEquals(0, value.get()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testToFunc1() { |
| 93 | + final AtomicLong value = new AtomicLong(-1L); |
| 94 | + final Action1<Integer> action = new Action1<Integer>() { |
| 95 | + @Override |
| 96 | + public void call(Integer t1) { |
| 97 | + value.set(t1); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + assertNull(Actions.toFunc(action).call(1)); |
| 102 | + assertEquals(1, value.get()); |
| 103 | + value.set(-1L); |
| 104 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1)); |
| 105 | + assertEquals(1, value.get()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testToFunc2() { |
| 110 | + final AtomicLong value = new AtomicLong(-1L); |
| 111 | + final Action2<Integer, Integer> action = new Action2<Integer, Integer>() { |
| 112 | + @Override |
| 113 | + public void call(Integer t1, Integer t2) { |
| 114 | + value.set(t1 | t2); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + assertNull(Actions.toFunc(action).call(1, 2)); |
| 119 | + assertNull(Actions.toFunc(action).call(1, 2)); |
| 120 | + assertEquals(3, value.get()); |
| 121 | + value.set(-1L); |
| 122 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2)); |
| 123 | + assertEquals(3, value.get()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testToFunc3() { |
| 128 | + final AtomicLong value = new AtomicLong(-1L); |
| 129 | + final Action3<Integer, Integer, Integer> action = new Action3<Integer, Integer, Integer>() { |
| 130 | + @Override |
| 131 | + public void call(Integer t1, Integer t2, Integer t3) { |
| 132 | + value.set(t1 | t2 | t3); |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + assertNull(Actions.toFunc(action).call(1, 2, 4)); |
| 137 | + assertEquals(7, value.get()); |
| 138 | + value.set(-1L); |
| 139 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4)); |
| 140 | + assertEquals(7, value.get()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testToFunc4() { |
| 145 | + final AtomicLong value = new AtomicLong(-1L); |
| 146 | + final Action4<Integer, Integer, Integer, Integer> action = new Action4<Integer, Integer, Integer, Integer>() { |
| 147 | + @Override |
| 148 | + public void call(Integer t1, Integer t2, Integer t3, Integer t4) { |
| 149 | + value.set(t1 | t2 | t3 | t4); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + assertNull(Actions.toFunc(action).call(1, 2, 4, 8)); |
| 154 | + assertEquals(15, value.get()); |
| 155 | + value.set(-1L); |
| 156 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4, 8)); |
| 157 | + assertEquals(15, value.get()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testToFunc5() { |
| 162 | + final AtomicLong value = new AtomicLong(-1L); |
| 163 | + final Action5<Integer, Integer, Integer, Integer, Integer> action = |
| 164 | + new Action5<Integer, Integer, Integer, Integer, Integer>() { |
| 165 | + @Override |
| 166 | + public void call(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5) { |
| 167 | + value.set(t1 | t2 | t3 | t4 | t5); |
| 168 | + } |
| 169 | + }; |
| 170 | + |
| 171 | + assertNull(Actions.toFunc(action).call(1, 2, 4, 8, 16)); |
| 172 | + assertEquals(31, value.get()); |
| 173 | + value.set(-1L); |
| 174 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4, 8, 16)); |
| 175 | + assertEquals(31, value.get()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testToFunc6() { |
| 180 | + final AtomicLong value = new AtomicLong(-1L); |
| 181 | + final Action6<Integer, Integer, Integer, Integer, Integer, Integer> action = |
| 182 | + new Action6<Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 183 | + @Override |
| 184 | + public void call(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6) { |
| 185 | + value.set(t1 | t2 | t3 | t4 | t5 | t6); |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + assertNull(Actions.toFunc(action).call(1, 2, 4, 8, 16, 32)); |
| 190 | + assertEquals(63, value.get()); |
| 191 | + value.set(-1L); |
| 192 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4, 8, 16, 32)); |
| 193 | + assertEquals(63, value.get()); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testToFunc7() { |
| 198 | + final AtomicLong value = new AtomicLong(-1L); |
| 199 | + final Action7<Integer, Integer, Integer, Integer, Integer, Integer, Integer> action = |
| 200 | + new Action7<Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 201 | + @Override |
| 202 | + public void call(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7) { |
| 203 | + value.set(t1 | t2 | t3 | t4 | t5 | t6 | t7); |
| 204 | + } |
| 205 | + }; |
| 206 | + |
| 207 | + assertNull(Actions.toFunc(action).call(1, 2, 4, 8, 16, 32, 64)); |
| 208 | + assertEquals(127, value.get()); |
| 209 | + value.set(-1L); |
| 210 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4, 8, 16, 32, 64)); |
| 211 | + assertEquals(127, value.get()); |
| 212 | + } |
| 213 | + @Test |
| 214 | + public void testToFunc8() { |
| 215 | + final AtomicLong value = new AtomicLong(-1L); |
| 216 | + final Action8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> action = |
| 217 | + new Action8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 218 | + @Override |
| 219 | + public void call(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8) { |
| 220 | + value.set(t1 | t2 | t3 | t4 | t5 | t6 | t7 | t8); |
| 221 | + } |
| 222 | + }; |
| 223 | + |
| 224 | + assertNull(Actions.toFunc(action).call(1, 2, 4, 8, 16, 32, 64, 128)); |
| 225 | + assertEquals(255, value.get()); |
| 226 | + value.set(-1L); |
| 227 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4, 8, 16, 32, 64, 128)); |
| 228 | + assertEquals(255, value.get()); |
| 229 | + } |
| 230 | + @Test |
| 231 | + public void testToFunc9() { |
| 232 | + final AtomicLong value = new AtomicLong(-1L); |
| 233 | + final Action9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> action = |
| 234 | + new Action9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 235 | + @Override |
| 236 | + public void call(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8, Integer t9) { |
| 237 | + value.set(t1 | t2 | t3 | t4 | t5 | t6 | t7 | t8 | t9); |
| 238 | + } |
| 239 | + }; |
| 240 | + |
| 241 | + assertNull(Actions.toFunc(action).call(1, 2, 4, 8, 16, 32, 64, 128, 256)); |
| 242 | + assertEquals(511, value.get()); |
| 243 | + value.set(-1L); |
| 244 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(1, 2, 4, 8, 16, 32, 64, 128, 256)); |
| 245 | + assertEquals(511, value.get()); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testToFuncN() { |
| 250 | + for (int i = 0; i < 100; i++) { |
| 251 | + final AtomicLong value = new AtomicLong(-1L); |
| 252 | + final ActionN action = new ActionN() { |
| 253 | + @Override |
| 254 | + public void call(Object... args) { |
| 255 | + int sum = 0; |
| 256 | + for (Object o : args) { |
| 257 | + sum += (Integer)o; |
| 258 | + } |
| 259 | + value.set(sum); |
| 260 | + } |
| 261 | + }; |
| 262 | + Object[] arr = new Object[i]; |
| 263 | + Arrays.fill(arr, 1); |
| 264 | + |
| 265 | + assertNull(Actions.toFunc(action).call(arr)); |
| 266 | + assertEquals(i, value.get()); |
| 267 | + value.set(-1L); |
| 268 | + assertEquals((Integer)0, Actions.toFunc(action, 0).call(arr)); |
| 269 | + assertEquals(i, value.get()); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + public void testNotInstantiable() { |
| 275 | + try { |
| 276 | + Constructor<?> c = Actions.class.getDeclaredConstructor(); |
| 277 | + c.setAccessible(true); |
| 278 | + Object instance = c.newInstance(); |
| 279 | + fail("Could instantiate Actions! " + instance); |
| 280 | + } catch (NoSuchMethodException ex) { |
| 281 | + ex.printStackTrace(); |
| 282 | + } catch (InvocationTargetException ex) { |
| 283 | + ex.printStackTrace(); |
| 284 | + } catch (InstantiationException ex) { |
| 285 | + ex.printStackTrace(); |
| 286 | + } catch (IllegalAccessException ex) { |
| 287 | + ex.printStackTrace(); |
| 288 | + } |
| 289 | + } |
| 290 | +} |
0 commit comments