11e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes/* 21e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * Copyright (C) 2010 The Android Open Source Project 3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 41e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 51e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * you may not use this file except in compliance with the License. 61e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * You may obtain a copy of the License at 7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 81e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 101e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * Unless required by applicable law or agreed to in writing, software 111e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 121e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * See the License for the specific language governing permissions and 141e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes * limitations under the License. 151e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes */ 161e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.util; 181e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes 194557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.util.Random; 201e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes 211e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughespublic class RandomTest extends junit.framework.TestCase { 221e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes public void test_subclassing() throws Exception { 231e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes // http://b/2502231 241e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes // Ensure that Random's constructors call setSeed by emulating the active ingredient 251e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes // from the bug: the subclass' setSeed had a side-effect necessary for the correct 261e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes // functioning of next. 271e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes class MyRandom extends Random { 281e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes public String state; 291e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes public MyRandom() { super(); } 301e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes public MyRandom(long l) { super(l); } 311e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes @Override protected synchronized int next(int bits) { return state.length(); } 321e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes @Override public synchronized void setSeed(long seed) { state = Long.toString(seed); } 331e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes } 341e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes // Test the 0-argument constructor... 351e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes MyRandom r1 = new MyRandom(); 361e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes r1.nextInt(); 371e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes assertNotNull(r1.state); 381e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes // Test the 1-argument constructor... 391e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes MyRandom r2 = new MyRandom(123L); 401e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes r2.nextInt(); 411e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes assertNotNull(r2.state); 421e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes } 431e8bd6143140f44337d6f567335afeb48b263d95Elliott Hughes} 44