Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testWasmLEB128.cpp
blob58ded9ab264deefaa52759ab5205c67066824491
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include <stdlib.h>
7 #include "jsapi-tests/tests.h"
9 #include "wasm/WasmValidate.h"
11 static bool WriteValidBytes(js::wasm::Encoder& encoder, bool* passed) {
12 *passed = false;
13 if (!encoder.empty()) {
14 return true;
17 // These remain the same under LEB128 unsigned encoding
18 if (!encoder.writeVarU32(0x0) || !encoder.writeVarU32(0x1) ||
19 !encoder.writeVarU32(0x42)) {
20 return false;
23 // 0x01 0x80
24 if (!encoder.writeVarU32(0x80)) {
25 return false;
28 // 0x03 0x80
29 if (!encoder.writeVarU32(0x180)) {
30 return false;
33 if (encoder.empty()) {
34 return true;
36 if (encoder.currentOffset() != 7) {
37 return true;
39 *passed = true;
40 return true;
43 BEGIN_TEST(testWasmLEB128_encoding) {
44 using namespace js;
45 using namespace wasm;
47 Bytes bytes;
48 Encoder encoder(bytes);
50 bool passed;
51 if (!WriteValidBytes(encoder, &passed)) {
52 return false;
54 CHECK(passed);
56 size_t i = 0;
57 CHECK(bytes[i++] == 0x0);
58 CHECK(bytes[i++] == 0x1);
59 CHECK(bytes[i++] == 0x42);
61 CHECK(bytes[i++] == 0x80);
62 CHECK(bytes[i++] == 0x01);
64 CHECK(bytes[i++] == 0x80);
65 CHECK(bytes[i++] == 0x03);
67 if (i + 1 < bytes.length()) {
68 CHECK(bytes[i++] == 0x00);
70 return true;
72 END_TEST(testWasmLEB128_encoding)
74 BEGIN_TEST(testWasmLEB128_valid_decoding) {
75 using namespace js;
76 using namespace wasm;
78 Bytes bytes;
79 if (!bytes.append(0x0) || !bytes.append(0x1) || !bytes.append(0x42)) {
80 return false;
83 if (!bytes.append(0x80) || !bytes.append(0x01)) {
84 return false;
87 if (!bytes.append(0x80) || !bytes.append(0x03)) {
88 return false;
92 // Fallible decoding
93 Decoder decoder(bytes);
94 uint32_t value;
96 CHECK(decoder.readVarU32(&value) && value == 0x0);
97 CHECK(decoder.readVarU32(&value) && value == 0x1);
98 CHECK(decoder.readVarU32(&value) && value == 0x42);
99 CHECK(decoder.readVarU32(&value) && value == 0x80);
100 CHECK(decoder.readVarU32(&value) && value == 0x180);
102 CHECK(decoder.done());
106 // Infallible decoding
107 Decoder decoder(bytes);
108 uint32_t value;
110 value = decoder.uncheckedReadVarU32();
111 CHECK(value == 0x0);
112 value = decoder.uncheckedReadVarU32();
113 CHECK(value == 0x1);
114 value = decoder.uncheckedReadVarU32();
115 CHECK(value == 0x42);
116 value = decoder.uncheckedReadVarU32();
117 CHECK(value == 0x80);
118 value = decoder.uncheckedReadVarU32();
119 CHECK(value == 0x180);
121 CHECK(decoder.done());
123 return true;
125 END_TEST(testWasmLEB128_valid_decoding)
127 BEGIN_TEST(testWasmLEB128_invalid_decoding) {
128 using namespace js;
129 using namespace wasm;
131 Bytes bytes;
132 // Fill bits as per 28 encoded bits
133 if (!bytes.append(0x80) || !bytes.append(0x80) || !bytes.append(0x80) ||
134 !bytes.append(0x80)) {
135 return false;
138 // Test last valid values
139 if (!bytes.append(0x00)) {
140 return false;
143 for (uint8_t i = 0; i < 0x0F; i++) {
144 bytes[4] = i;
147 Decoder decoder(bytes);
148 uint32_t value;
149 CHECK(decoder.readVarU32(&value));
150 CHECK(value == uint32_t(i << 28));
151 CHECK(decoder.done());
155 Decoder decoder(bytes);
156 uint32_t value = decoder.uncheckedReadVarU32();
157 CHECK(value == uint32_t(i << 28));
158 CHECK(decoder.done());
162 // Test all invalid values of the same size
163 for (uint8_t i = 0x10; i < 0xF0; i++) {
164 bytes[4] = i;
166 Decoder decoder(bytes);
167 uint32_t value;
168 CHECK(!decoder.readVarU32(&value));
171 return true;
173 END_TEST(testWasmLEB128_invalid_decoding)