Bug 1716030 [wpt PR 29346] - Update wpt metadata, a=testonly
[gecko.git] / js / src / jsapi-tests / testSparseBitmap.cpp
blobbd5e7fee95876941730d341b4dfefddb5f14aeb7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/PodOperations.h"
10 #include "ds/Bitmap.h"
12 #include "jsapi-tests/tests.h"
14 using namespace js;
16 BEGIN_TEST(testSparseBitmapBasics) {
17 SparseBitmap bitmap;
19 // Test bits in first block are initially zero.
20 for (size_t i = 0; i < 100; i++) {
21 CHECK(!bitmap.getBit(i));
24 // Test bits in different blocks are initially zero.
25 for (size_t i = 0; i < 100; i++) {
26 CHECK(!bitmap.getBit(i * 1000));
29 // Set some bits in the first block and check they are set.
30 for (size_t i = 0; i < 100; i += 2) {
31 bitmap.setBit(i);
33 for (size_t i = 0; i < 100; i++) {
34 CHECK(bitmap.getBit(i) == ((i % 2) == 0));
37 // Set some bits in different blocks and check they are set.
38 for (size_t i = 0; i < 100; i += 2) {
39 bitmap.setBit(i * 1000);
41 for (size_t i = 0; i < 100; i++) {
42 CHECK(bitmap.getBit(i * 1000) == ((i % 2) == 0));
45 // Create another bitmap with different bits set.
46 SparseBitmap other;
47 for (size_t i = 1; i < 100; i += 2) {
48 other.setBit(i * 1000);
50 for (size_t i = 0; i < 100; i++) {
51 CHECK(other.getBit(i * 1000) == ((i % 2) != 0));
54 // OR some bits into this bitmap and check the result.
55 bitmap.bitwiseOrWith(other);
56 for (size_t i = 0; i < 100; i++) {
57 CHECK(bitmap.getBit(i * 1000));
60 // AND some bits into this bitmap and check the result.
61 DenseBitmap dense;
62 size_t wordCount = (100 * 1000) / JS_BITS_PER_WORD + 1;
63 CHECK(dense.ensureSpace(wordCount));
64 other.bitwiseOrInto(dense);
65 bitmap.bitwiseAndWith(dense);
66 for (size_t i = 0; i < 100; i++) {
67 CHECK(bitmap.getBit(i * 1000) == ((i % 2) != 0));
70 return true;
72 END_TEST(testSparseBitmapBasics)
74 BEGIN_TEST(testSparseBitmapExternalOR) {
75 // Testing ORing data into an external array.
77 const size_t wordCount = 10;
79 // Create a bitmap with one bit set per word so we can tell them apart.
80 SparseBitmap bitmap;
81 for (size_t i = 0; i < wordCount; i++) {
82 bitmap.setBit(i * JS_BITS_PER_WORD + i);
85 // Copy a single word.
86 uintptr_t target[wordCount];
87 mozilla::PodArrayZero(target);
88 bitmap.bitwiseOrRangeInto(0, 1, target);
89 CHECK(target[0] == 1u << 0);
90 CHECK(target[1] == 0);
92 // Copy a word at an offset.
93 mozilla::PodArrayZero(target);
94 bitmap.bitwiseOrRangeInto(1, 1, target);
95 CHECK(target[0] == 1u << 1);
96 CHECK(target[1] == 0);
98 // Check data is ORed with original target contents.
99 mozilla::PodArrayZero(target);
100 bitmap.bitwiseOrRangeInto(0, 1, target);
101 bitmap.bitwiseOrRangeInto(1, 1, target);
102 CHECK(target[0] == ((1u << 0) | (1u << 1)));
104 // Copy multiple words at an offset.
105 mozilla::PodArrayZero(target);
106 bitmap.bitwiseOrRangeInto(2, wordCount - 2, target);
107 for (size_t i = 0; i < wordCount - 2; i++) {
108 CHECK(target[i] == (1u << (i + 2)));
110 CHECK(target[wordCount - 1] == 0);
112 return true;
115 END_TEST(testSparseBitmapExternalOR)