1 /* 2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.runtime.test; 27 28 import jdk.nashorn.internal.runtime.ConsString; 29 import static org.testng.Assert.assertEquals; 30 31 import org.testng.annotations.Test; 32 33 /** 34 * Tests for JSType methods. 35 * 36 * @test 37 * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime 38 * @run testng jdk.nashorn.internal.runtime.test.ConsStringTest 39 */ 40 public class ConsStringTest { 41 42 /** 43 * Test toString conversion 44 */ 45 @Test 46 public void testConsStringToString() { 47 final ConsString cs1 = new ConsString("b", "c"); 48 final ConsString cs2 = new ConsString("d", "e"); 49 final ConsString cs3 = new ConsString(cs1, cs2); 50 final ConsString cs4 = new ConsString(cs3, "f"); 51 final ConsString cs5 = new ConsString("a", cs4); 52 assertEquals(cs5.toString(), "abcdef"); 53 assertEquals(cs4.toString(), "bcdef"); 54 assertEquals(cs3.toString(), "bcde"); 55 assertEquals(cs2.toString(), "de"); 56 assertEquals(cs1.toString(), "bc"); 57 // ConsStrings should be flattened now 58 assertEquals(cs1.getComponents()[0], "bc"); 59 assertEquals(cs1.getComponents()[1], ""); 60 assertEquals(cs2.getComponents()[0], "de"); 61 assertEquals(cs2.getComponents()[1], ""); 62 assertEquals(cs3.getComponents()[0], "bcde"); 63 assertEquals(cs3.getComponents()[1], ""); 64 assertEquals(cs4.getComponents()[0], "bcdef"); 65 assertEquals(cs4.getComponents()[1], ""); 66 assertEquals(cs5.getComponents()[0], "abcdef"); 67 assertEquals(cs5.getComponents()[1], ""); 68 } 69 70 /** 71 * Test charAt 72 */ 73 @Test 74 public void testConsStringCharAt() { 75 final ConsString cs1 = new ConsString("b", "c"); 76 final ConsString cs2 = new ConsString("d", "e"); 77 final ConsString cs3 = new ConsString(cs1, cs2); 78 final ConsString cs4 = new ConsString(cs3, "f"); 79 final ConsString cs5 = new ConsString("a", cs4); 80 assertEquals(cs1.charAt(1), 'c'); 81 assertEquals(cs2.charAt(0), 'd'); 82 assertEquals(cs3.charAt(3), 'e'); 83 assertEquals(cs4.charAt(1), 'c'); 84 assertEquals(cs5.charAt(2), 'c'); 85 // ConsStrings should be flattened now 86 assertEquals(cs1.getComponents()[0], "bc"); 87 assertEquals(cs1.getComponents()[1], ""); 88 assertEquals(cs2.getComponents()[0], "de"); 89 assertEquals(cs2.getComponents()[1], ""); 90 assertEquals(cs3.getComponents()[0], "bcde"); 91 assertEquals(cs3.getComponents()[1], ""); 92 assertEquals(cs4.getComponents()[0], "bcdef"); 93 assertEquals(cs4.getComponents()[1], ""); 94 assertEquals(cs5.getComponents()[0], "abcdef"); 95 assertEquals(cs5.getComponents()[1], ""); 96 } 97 98 99 /** 100 * Test flattening of top-level and internal ConsStrings 101 */ 102 @Test 103 public void testConsStringFlattening() { 104 final ConsString cs1 = new ConsString("b", "c"); 105 final ConsString cs2 = new ConsString("d", "e"); 106 final ConsString cs3 = new ConsString(cs1, cs2); 107 final ConsString cs4 = new ConsString(cs3, "f"); 108 109 final ConsString cs5 = new ConsString("a", cs4); 110 // top-level ConsString should not yet be flattened 111 assert(cs5.getComponents()[0] == "a"); 112 assert(cs5.getComponents()[1] == cs4); 113 assertEquals(cs5.toString(), "abcdef"); 114 // top-level ConsString should be flattened 115 assertEquals(cs5.getComponents()[0], "abcdef"); 116 assertEquals(cs5.getComponents()[1], ""); 117 // internal ConsString should not yet be flattened after first traversal 118 assertEquals(cs4.getComponents()[0], cs3); 119 assertEquals(cs4.getComponents()[1], "f"); 120 121 final ConsString cs6 = new ConsString("a", cs4); 122 // top-level ConsString should not yet be flattened 123 assertEquals(cs6.getComponents()[0], "a"); 124 assertEquals(cs6.getComponents()[1], cs4); 125 assertEquals(cs6.toString(), "abcdef"); 126 // top-level ConsString should be flattened 127 assertEquals(cs6.getComponents()[0], "abcdef"); 128 assertEquals(cs6.getComponents()[1], ""); 129 // internal ConsString should have been flattened after second traversal 130 assertEquals(cs4.getComponents()[0], "bcdef"); 131 assertEquals(cs4.getComponents()[1], ""); 132 } 133 }