1/* 2 * Copyright (c) 2024 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 16class LogEntry { 17 value; 18 index; 19 array; 20} 21const testCases = [ 22 () => { 23 let log = []; 24 const result = [1, 2, 3, 4].every((x, i, arr) => { 25 log.push({ value: x, index: i, array: [...arr] }); 26 return x > 0; 27 }); 28 print("Log:", JSON.stringify(log), "Result:", result); 29 }, 30 31 () => { 32 let log = []; 33 const result = [1, 2, -3, 4].every((x, i, arr) => { 34 log.push({ value: x, index: i, array: [...arr] }); 35 return x > 0; 36 }); 37 print("Log:", JSON.stringify(log), "Result:", result); 38 }, 39 40 () => { 41 let log = []; 42 const result = [].every((x, i, arr) => { 43 log.push({ value: x, index: i, array: [...arr] }); 44 return x > 0; 45 }); 46 print("Log:", JSON.stringify(log), "Result:", result); 47 }, 48 49 () => { 50 let log = []; 51 const result = [10].every((x, i, arr) => { 52 log.push({ value: x, index: i, array: [...arr] }); 53 return x > 0; 54 }); 55 print("Log:", JSON.stringify(log), "Result:", result); 56 }, 57 58 () => { 59 let log = []; 60 const result = [, , 3, , 5].every((x, i, arr) => { 61 log.push({ value: x, index: i, array: [...arr] }); 62 return x !== undefined; 63 }); 64 print("Log:", JSON.stringify(log), "Result:", result); 65 }, 66 67 () => { 68 let log = []; 69 const result = [1, undefined, 3].every((x, i, arr) => { 70 log.push({ value: x, index: i, array: [...arr] }); 71 return x > 0; 72 }); 73 print("Log:", JSON.stringify(log), "Result:", result); 74 }, 75 76 () => { 77 let log = []; 78 const result = [null, 1, 2].every((x, i, arr) => { 79 log.push({ value: x, index: i, array: [...arr] }); 80 return x !== null; 81 }); 82 print("Log:", JSON.stringify(log), "Result:", result); 83 }, 84 85 () => { 86 let log = []; 87 const result = [NaN, 1, 2].every((x, i, arr) => { 88 log.push({ value: x, index: i, array: [...arr] }); 89 return !isNaN(x); 90 }); 91 print("Log:", JSON.stringify(log), "Result:", result); 92 }, 93 94 () => { 95 let log = []; 96 let arr = [1, 2]; 97 const result = arr.every((x, i, arrRef) => { 98 log.push({ value: x, index: i, array: [...arrRef] }); 99 if (i === 1) arrRef.push(3); 100 return x > 0; 101 }); 102 print("Log:", JSON.stringify(log), "Result:", result); 103 }, 104 105 () => { 106 let log = []; 107 let arr = [1, 2, 3]; 108 const result = arr.every((x, i, arrRef) => { 109 log.push({ value: x, index: i, array: [...arrRef] }); 110 if (i === 1) arrRef.pop(); 111 return x > 0; 112 }); 113 print("Log:", JSON.stringify(log), "Result:", result); 114 }, 115 116 () => { 117 let log = []; 118 const result = [false, 0, ''].every((x, i, arr) => { 119 log.push({ value: x, index: i, array: [...arr] }); 120 return !x; 121 }); 122 print("Log:", JSON.stringify(log), "Result:", result); 123 }, 124 125 () => { 126 let log = []; 127 const result = [1, 2, 3].every((x, i, arr) => { 128 log.push({ value: x, index: i, array: [...arr] }); 129 return true; 130 }); 131 print("Log:", JSON.stringify(log), "Result:", result); 132 }, 133 134 () => { 135 let log = []; 136 const result = [1, 2, 3].every((x, i, arr) => { 137 log.push({ value: x, index: i, array: [...arr] }); 138 return false; 139 }); 140 print("Log:", JSON.stringify(log), "Result:", result); 141 }, 142 143 () => { 144 let log = []; 145 const result = [[1, 2], [3, 4]].every((subArray, i, arr) => { 146 log.push({ value: subArray, index: i, array: [...arr] }); 147 return subArray.every(x => x > 0); 148 }); 149 print("Log:", JSON.stringify(log), "Result:", result); 150 }, 151 152 () => { 153 let log = []; 154 let obj = { x: 1 }; 155 const result = [obj, obj].every((x, i, arr) => { 156 log.push({ value: x, index: i, array: [...arr] }); 157 x.x++; 158 return x.x > 1; 159 }); 160 print("Log:", JSON.stringify(log), "Result:", result); 161 }, 162 163 () => { 164 let log = []; 165 const context = { limit: 5 }; 166 const result = [1, 2, 3].every(function (x, i, arr) { 167 log.push({ value: x, index: i, array: [...arr] }); 168 return x < this.limit; 169 }, context); 170 print("Log:", JSON.stringify(log), "Result:", result); 171 }, 172 173 () => { 174 let log = []; 175 try { 176 [1, 2, 3].every((x, i, arr) => { 177 log.push({ value: x, index: i, array: [...arr] }); 178 if (x === 2) throw new Error('Test Error'); 179 return x > 0; 180 }); 181 } catch (e) { 182 print("Log:", JSON.stringify(log), "Error:", e.message); 183 } 184 }, 185 186 () => { 187 let log = []; 188 const result = [{ id: 1 }, { id: 2 }].every((x, i, arr) => { 189 log.push({ value: x, index: i, array: [...arr] }); 190 return x.id > 0; 191 }); 192 print("Log:", JSON.stringify(log), "Result:", result); 193 }, 194 195 // Callback modifies the array during traversal (removing elements then add) 196 () => { 197 let log = []; 198 let arr = [1, 2, 3, 4]; 199 const result = arr.every((x, i, arrRef) => { 200 log.push({ value: x, index: i, array: [...arrRef] }); 201 if (i === 1) arrRef.pop(); // Remove the last element 202 if (i === 2) arrRef.push(5); // Add the last element 203 return x > 0; 204 }); 205 print("Log:", JSON.stringify(log), "Result:", result); 206 }, 207]; 208 209testCases.forEach((test, i) => { 210 print(`Test case ${i + 1}:`); 211 test(); 212 print('---'); 213}); 214