RandomSequence { public static int[] randomIntSequence(int n) throws SequenceException { if (n <= 0) { throw new SequenceException("产生随机序列范围值小于等于0"); } int num[] = new int[n]; for (int i = 0; i < n; i++) { num[i] = i; } if (n == 1) { return num; } Random random = new Random(); if (n == 2 && random.nextInt(2) == 1) { int temp = num[0]; num[0] = num[1]; num[1] = temp; } int[] result = randomIntSequence(num); return result; }  static int[] randomIntSequence(int n); static int[] randomIntSequence(int[] sourceQueue);  }
@Test public void test_randomIntSequence() { try { RandomSequence.randomIntSequence(0); Assert.assertTrue(false); } catch (SequenceException e) { Assert.assertTrue(true); } try { RandomSequence.randomIntSequence(-1); Assert.assertTrue(false); } catch (SequenceException e) { Assert.assertTrue(true); } int[] random1; try { random1 = RandomSequence.randomIntSequence(1); Assert.assertEquals(random1[0], 0); } catch (SequenceException e) { } }