1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:stringSlice 18 * @tc.desc:test String.slice 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22 23function testLocaleCompare() { 24 const testCases = [ 25 // LineOrConstant v.s. LineOrConstant 26 { str1: 'apple', str2: 'apple' }, 27 { str1: 'apple', str2: 'banana' }, 28 { str1: 'banana', str2: 'apple' }, 29 { str1: 'app', str2: 'apple' }, 30 { str1: 'application', str2: 'apple' }, 31 { str1: 'Apple', str2: 'apple' }, 32 { str1: '123', str2: '1234' }, 33 { str1: '1234', str2: '123' }, 34 { str1: 'Zebra', str2: 'apple' }, 35 { str1: 'appleappleappleappleappleapple', str2: 'appleappleappleappleappleapple' }, 36 { str1: 'appleappleappleappleappleapple', str2: 'appleappleapplebppleappleapple' }, 37 { str1: 'appleappleappleAppleappleapple', str2: 'appleappleappleappleappleapple' }, 38 // SliceString v.s. SliceString 39 { str1: 'appleappleapple'.substring(0, 5), str2: 'appleappleapple'.substring(5, 10) }, 40 { str1: 'appleappleapple'.substring(1, 6), str2: 'appleappleapple'.substring(6, 11) }, 41 { str1: 'appleappleapple'.substring(0, 5), str2: 'appleappleapple'.substring(10, 15) }, 42 { str1: 'appleappleapple'.substring(0, 5), str2: 'appleappleapple'.substring(5, 11) }, 43 // LineOrConstant v.s. TreeString 44 { str1: 'HelloWorld', str2: 'Hello'.concat('World') }, 45 { str1: 'Ni'.concat('Hao', 'Ya'), str2: 'NiHaoYa' }, 46 { str1: 'HelloWorld', str2: 'Hello'.concat('World ') }, 47 { str1: 'Ni'.concat('Hao', 'Ya', '1'), str2: 'NiHaoYa' }, 48 // TreeString v.s. TreeString 49 { str1: 'Hell'.concat('oWorld'), str2: 'Hello'.concat('World') }, 50 { str1: 'Ni'.concat('Hao', 'Ya'), str2: 'NiHao'.concat('Ya') }, 51 { str1: 'Hello'.concat('a'), str2: 'Hello'.concat('b') }, 52 { str1: 'Ni'.concat('Hao', '8'), str2: 'NiHao'.concat('7') }, 53 // TreeString v.s. SliceString 54 { str1: 'appleappleapple'.substring(5, 10), str2: 'app'.concat('le') }, 55 { str1: 'hello'.concat('world'), str2: 'ohelloworld'.substring(1) }, 56 { str1: 'hello'.concat('world'), str2: 'ohelloworld'.substring(2) }, 57 { str1: 'appleappleapple'.substring(5, 11), str2: 'app'.concat('le') }, 58 // LineOrConstant v.s. SliceString 59 { str1: 'apple', str2: 'appleappleapple'.substring(5, 10) }, 60 { str1: 'appleappleapple'.substring(0, 5), str2: 'apple' }, 61 // Complex string compare 62 { str1: 'hello'.concat('world').substring(2, 4), str2: 'hello'.substring(2, 3).concat('l') }, 63 ]; 64 testCases.forEach(testCase => { 65 // default locale is en-US 66 const result = testCase.str1.localeCompare(testCase.str2); 67 print(result); 68 }); 69 const testCasesUni = [ 70 // UTF8 v.s. UTF16 71 { str1: 'café', str2: 'cafe' }, 72 { str1: 'café', str2: 'café' }, 73 { str1: 'resumé', str2: 'resume' }, 74 { str1: 'resume', str2: 'resumé' }, 75 { str1: ' naïve', str2: 'naïve' }, 76 { str1: 'naïve', str2: ' naïve' }, 77 { str1: 'élan', str2: 'elan' }, 78 { str1: 'élan', str2: 'e' + String.fromCharCode(0x0301) + 'lan' } 79 ]; 80 testCasesUni.forEach(testCase => { 81 const result = testCase.str1.localeCompare(testCase.str2, 'en-US'); 82 print(result); 83 }); 84 } 85 testLocaleCompare(); 86 87