Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions opal/class/opal_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Copyright (c) 2014 Intel, Inc. All rights reserved.
* Copyright (c) 2015-2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand Down Expand Up @@ -361,15 +362,21 @@ int opal_bitmap_num_unset_bits(opal_bitmap_t *bm, int len)
int opal_bitmap_num_set_bits(opal_bitmap_t *bm, int len)
{
int i, cnt = 0;
int num_elements, remaining_bits;
uint64_t val;

#if OPAL_ENABLE_DEBUG
if ((len < 0) || NULL == bm || (len >= (bm->array_size * SIZE_OF_BASE_TYPE))) {
if ((len < 0) || NULL == bm || (len > (bm->array_size * SIZE_OF_BASE_TYPE))) {
return 0;
}
#endif

for (i = 0; i < len; ++i) {
/* Calculate how many full array elements to process */
num_elements = len / SIZE_OF_BASE_TYPE;
remaining_bits = len % SIZE_OF_BASE_TYPE;

/* Count bits in full elements */
for (i = 0; i < num_elements; ++i) {
if (0 == (val = bm->bitmap[i])) {
continue;
}
Expand All @@ -380,6 +387,16 @@ int opal_bitmap_num_set_bits(opal_bitmap_t *bm, int len)
}
}

/* Handle the last partial element if there are remaining bits */
if (remaining_bits > 0) {
val = bm->bitmap[num_elements];
/* Mask off bits beyond len */
val &= ((1UL << remaining_bits) - 1);
for (; val; cnt++) {
val &= val - 1; /* clear the least significant bit set */
}
}

return cnt;
}

Expand Down
97 changes: 97 additions & 0 deletions test/class/opal_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static void test_bitmap_is_set(opal_bitmap_t *bm);
static void test_bitmap_clear_all(opal_bitmap_t *bm);
static void test_bitmap_set_all(opal_bitmap_t *bm);
static void test_bitmap_find_and_set(opal_bitmap_t *bm);
static void test_bitmap_num_set_bits(opal_bitmap_t *bm);

static int set_bit(opal_bitmap_t *bm, int bit);
static int clear_bit(opal_bitmap_t *bm, int bit);
Expand Down Expand Up @@ -93,6 +94,9 @@ int main(int argc, char *argv[])
fprintf(error_out, "\nTesting bitmap find_and_set... \n");
test_bitmap_find_and_set(&bm);

fprintf(error_out, "\nTesting bitmap num_set_bits... \n");
test_bitmap_num_set_bits(&bm);

fprintf(error_out, "\n~~~~~~ Testing complete ~~~~~~ \n\n");

test_finalize();
Expand Down Expand Up @@ -216,6 +220,99 @@ void test_bitmap_set_all(opal_bitmap_t *bm)
TEST_AND_REPORT(result, 0, " error in opal_bitmap_set_ala_bitsl");
}

void test_bitmap_num_set_bits(opal_bitmap_t *bm)
{
int result, expected;

/* Test 1: Clear all bits and count - should be 0 */
opal_bitmap_clear_all_bits(bm);
result = opal_bitmap_num_set_bits(bm, 64);
expected = 0;
TEST_AND_REPORT(result, expected, "opal_bitmap_num_set_bits: cleared bitmap");

/* Test 2: Set specific bits and count within first 64 bits */
opal_bitmap_set_bit(bm, 0);
opal_bitmap_set_bit(bm, 1);
opal_bitmap_set_bit(bm, 5);
opal_bitmap_set_bit(bm, 63);
result = opal_bitmap_num_set_bits(bm, 64);
expected = 4;
TEST_AND_REPORT(result, expected, "opal_bitmap_num_set_bits: 4 bits in first 64");

/* Test 3: Count partial element (len not a multiple of 64) */
/* Count only first 10 bits - should be 3 (bits 0, 1, 5) */
result = opal_bitmap_num_set_bits(bm, 10);
expected = 3;
TEST_AND_REPORT(result, expected, "opal_bitmap_num_set_bits: partial element (10 bits)");

/* Test 4: Count up to bit 63 - should include bit 63 */
result = opal_bitmap_num_set_bits(bm, 64);
expected = 4;
TEST_AND_REPORT(result, expected, "opal_bitmap_num_set_bits: up to bit 63");

/* Test 5: Set bits across multiple array elements */
opal_bitmap_set_bit(bm, 64);
opal_bitmap_set_bit(bm, 65);
opal_bitmap_set_bit(bm, 100);
opal_bitmap_set_bit(bm, 127);

/* Count across 128 bits (2 full elements) */
result = opal_bitmap_num_set_bits(bm, 128);
expected = 8; /* 4 from first element + 4 from second element */
TEST_AND_REPORT(result, expected, "opal_bitmap_num_set_bits: across 2 elements (128 bits)");

/* Test 6: Count partial second element (130 bits = 2 elements + 2 bits) */
result = opal_bitmap_num_set_bits(bm, 130);
expected = 8; /* Should still be 8, as bits 128-129 are not set */
TEST_AND_REPORT(result, expected,
"opal_bitmap_num_set_bits: partial second element (130 bits)");

/* Test 7: Verify len is treated as bits, not array indices
* Set bit 200 and count up to 201 bits (not 201 array elements) */
opal_bitmap_clear_all_bits(bm);
opal_bitmap_set_bit(bm, 200);
result = opal_bitmap_num_set_bits(bm, 201);
expected = 1;
TEST_AND_REPORT(result, expected,
"opal_bitmap_num_set_bits: len treated as bits (201 bits)");

/* Test 8: Count up to 200 should not include bit 200 */
result = opal_bitmap_num_set_bits(bm, 200);
expected = 0;
TEST_AND_REPORT(result, expected,
"opal_bitmap_num_set_bits: len boundary not included (200 bits)");

/* Test 9: Set all bits in first element and count partial */
opal_bitmap_clear_all_bits(bm);
for (int i = 0; i < 64; ++i) {
opal_bitmap_set_bit(bm, i);
}
/* Count only first 50 bits - should be 50 */
result = opal_bitmap_num_set_bits(bm, 50);
expected = 50;
TEST_AND_REPORT(result, expected,
"opal_bitmap_num_set_bits: partial full element (50 of 64 bits)");

/* Test 10: Edge case - count exactly one full element */
result = opal_bitmap_num_set_bits(bm, 64);
expected = 64;
TEST_AND_REPORT(result, expected,
"opal_bitmap_num_set_bits: exactly one element (64 bits)");

/* Test 11: Test opal_bitmap_num_unset_bits consistency */
opal_bitmap_clear_all_bits(bm);
opal_bitmap_set_bit(bm, 5);
opal_bitmap_set_bit(bm, 10);
opal_bitmap_set_bit(bm, 15);
result = opal_bitmap_num_set_bits(bm, 64);
expected = 3;
TEST_AND_REPORT(result, expected, "opal_bitmap_num_set_bits: 3 set bits in 64");

result = opal_bitmap_num_unset_bits(bm, 64);
expected = 61; /* 64 - 3 = 61 */
TEST_AND_REPORT(result, expected, "opal_bitmap_num_unset_bits: 61 unset bits in 64");
}

int set_bit(opal_bitmap_t *bm, int bit)
{
int err = opal_bitmap_set_bit(bm, bit);
Expand Down
3 changes: 2 additions & 1 deletion test/support/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates.
* All Rights reserved.
* Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand Down Expand Up @@ -45,7 +46,7 @@
}

test_verify_number(int, "%d")
test_verify_number(int64_t, "%ld")
test_verify_number(int64_t, "%" PRId64)
test_verify_number(size_t, "%lu")
test_verify_number(double, "%lf")

Expand Down
18 changes: 10 additions & 8 deletions test/util/opal_json.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2024-2025 Amazon.com, Inc. or its affiliates.
* All Rights reserved.
* Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand Down Expand Up @@ -238,16 +239,17 @@ static void test_valid_json(void)
static void test_invalid_json_string(void)
{
int ret = 0;
static const int cnt = 3;
char *test_cases[cnt];
static const char *test_cases[] = {
"1,2,3",
"{a: 1}",
// This string should fail, but does not.
// Commenting out for now.
//"[1,2,3,]",
};

test_cases[0] = "1,2,3";
test_cases[1] = "[1,2,3,]";
test_cases[2] = "{a: 1}";

for (int i = 0; i < 1; ++i) {
for (int i = 0; i < (int)(sizeof(test_cases) / sizeof(test_cases[0])); ++i) {
const opal_json_t *json = NULL;
char *test_case = test_cases[i];
const char *test_case = test_cases[i];
for (int input_type = JSON_STRING; input_type < INPUT_TYPE_COUNT; ++input_type) {
ret = load_json(test_case, input_type, &json);
if (ret) {
Expand Down
Loading