• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2023-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
16const { getTestClass } = require('ets_proxy.test.js');
17const PrimitivesAccess = getTestClass('PrimitivesAccess');
18const PrimitivesAccessStatic = getTestClass('PrimitivesAccessStatic');
19
20let pa = new PrimitivesAccess();
21let pas = PrimitivesAccessStatic;
22
23let typelist = ['byte', 'short', 'int', 'long', 'float', 'double', 'char', 'boolean'];
24const cap = (str = '') => str[0].toUpperCase() + str.substring(1);
25
26{
27	// Test property accessors
28	function testAccessors(tname, ...values) {
29		function testAccessorsOf(o, tname, ...values) {
30			for (let v of values) {
31				o['f' + cap(tname)] = v;
32				ASSERT_EQ(o['getf' + cap(tname)](), v);
33				o['setf' + cap(tname)](v);
34				ASSERT_EQ(o['f' + cap(tname)], v);
35			}
36		}
37		testAccessorsOf(pa, tname, ...values);
38		testAccessorsOf(pas, tname, ...values);
39	}
40
41	function intBit(exp) {
42		ASSERT_TRUE(exp < 53, 'intBit overflow');
43		return (1 << exp % 30) * (1 << (exp - (exp % 30)));
44	}
45	function testSInt(tname, bits) {
46		let msb = bits - 1;
47		testAccessors(tname, 0, 1, -1, intBit(msb) - 1, -intBit(msb));
48	}
49	function testUInt(tname, bits) {
50		let msb = bits - 1;
51		testAccessors(tname, 0, 1, intBit(msb), intBit(msb + 1) - 1);
52	}
53
54	testSInt('byte', 8);
55	testSInt('short', 16);
56	testSInt('int', 32);
57	testSInt('long', 53);
58	testAccessors('float', 0, 1, 1.25, 0x1234 / 256, Infinity, NaN);
59	testAccessors('double', 0, 1, 1.33333, 0x123456789a / 256, Infinity, NaN);
60	testUInt('char', 16);
61	testAccessors('boolean', false, true);
62}
63
64{
65	// Test typechecks
66	typelist.forEach(function (tname) {
67		function check(o) {
68			ASSERT_THROWS(TypeError, () => (o['f' + cap(tname)] = undefined));
69		}
70		check(pa);
71		check(pas);
72	});
73}
74