1/* 2 * Copyright (c) 2021-2025 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/* 18 Sample Program which demonstrate and test ArkTS features 19 as they described in ArkTS Tutorial 20*/ 21 22class Point { 23 internal x: double; 24 internal y: double; 25 constructor() { 26 this.x = Double.NaN; 27 this.y = Double.NaN; 28 } 29 constructor(x: double, y: double) { 30 this.x = x; 31 this.y = y; 32 } 33} 34 35 36interface Figure { 37 // currently not usable 38 private fakeId(): int { return 13; } 39 getOrigin(): Point; 40 getWidth(): double; 41 getHeight(): double; 42 // default realisation to test override 43 id(): int { return 13; }; 44 intersects(other: Figure | null): boolean { return false; }; 45} 46 47 48class Rectangle implements Figure { 49 static total: int = 0; 50 start: Point | null; // bottom-left 51 end: Point | null; // top-right 52 constructor() { 53 this.start = null; 54 this.end = null; 55 Rectangle.total++; 56 } 57 constructor(start: Point, end: Point) { 58 this.start = start; 59 this.end = end; 60 Rectangle.total++; 61 } 62 override getOrigin(): Point { 63 return this.start ?? new Point(); 64 } 65 override getWidth(): double { 66 let s = this.start ?? new Point(); 67 let e = this.end ?? new Point(); 68 return abs(e.x - s.x); 69 } 70 override getHeight(): double { 71 let s = this.start ?? new Point(); 72 let e = this.end ?? new Point(); 73 return abs(e.y - s.y); 74 } 75} 76 77 78final class Square extends Rectangle { 79 constructor() { 80 super(); 81 } 82 constructor(start: Point, side: double) { 83 // TODO: start?.x 84 super(start, new Point(start.x + side, start.y + side)); 85 } 86 // Figure::intersects(other: Figure | null): boolean 87 override intersects(other: Rectangle | null): boolean { 88 if (null === other) { 89 return false; 90 } 91 if (this.end.y < other.start.y 92 || this.start.y > other.end.y) { 93 return false; 94 } 95 if (this.end.x < other.start.x 96 || this.start.x > other.end.x) { 97 return false; 98 } 99 return true; 100 } 101} 102 103function test(result: boolean, name: String): int { 104 if (result) { 105 console.println("PASSED: " + name); 106 return 0; 107 } 108 console.println("FAILED: " + name); 109 return 1; 110} 111 112function default_ctor(): boolean { 113 let total: int = Rectangle.total; 114 let r = new Rectangle(); 115 return (Rectangle.total == total + 1) 116 && (Double.isNaN(r.getOrigin().x)) 117 && (Double.isNaN(r.getOrigin().y)) 118 && (Double.isNaN(r.getWidth())) 119 && (Double.isNaN(r.getHeight())) 120 && (r.id() == 13); 121} 122 123function rect_ctor(): boolean { 124 let r = new Rectangle(new Point(5, 10), new Point(6, 12)); 125 return (5 == r.getOrigin().x) 126 && (10 == r.getOrigin().y) 127 && (1 == r.getWidth()) 128 && (2 == r.getHeight()) 129 && (r.id() == 13); 130} 131 132function default_iface_method(): boolean { 133 let r1 = new Rectangle(); 134 let r2 = new Rectangle(); 135 return !r1.intersects(r2); 136} 137 138function square_ctor(): boolean { 139 let r = new Square(new Point(5, 10), 8); 140 return (5 == r.getOrigin().x) 141 && (10 == r.getOrigin().y) 142 && (8 == r.getWidth()) 143 && (8 == r.getHeight()) 144 && (r.id() == 13); 145} 146 147function square_override(): boolean { 148 let s1 = new Square(new Point(0, 0), 10); 149 let s2 = new Square(new Point(-5, -5), 100); 150 let r1 = new Rectangle(new Point(1, 2), new Point(2, 4)); 151 let r2 = new Rectangle(new Point(-2, -2), new Point(-1, -1)); 152 return (s1.intersects(s2)) 153 && (s1.intersects(r1)) 154 && (!s1.intersects(r2)); 155} 156 157function array_of_squares(): boolean { 158 let a: Square[] = new Square[32]; 159 let ok: boolean = true; 160 for (let i = 0; i < a.length; i++) { 161 a[i] = new Square(new Point(2.0*i, 2.0*i), 3); 162 } 163 for (let i = 0; i < a.length - 1; i++) { 164 ok = ok && a[i].intersects(a[i+1]); 165 } 166 return ok; 167} 168 169function main(): int { 170 let failures: int = 0; 171 failures += test(default_ctor(), "Sample App default ctor"); 172 failures += test(rect_ctor(), "Sample App super ctor"); 173 failures += test(default_iface_method(), "Sample App default iface method"); 174 failures += test(square_ctor(), "Sample App extend ctor"); 175 failures += test(square_override(), "Sample App override method"); 176 failures += test(array_of_squares(), "Sample App array of user types"); 177 if (failures == 0) { 178 console.println("PASSED: All tests run"); 179 } else { 180 console.println("FAILED: All tests run"); 181 } 182 return failures; 183} 184