| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2024 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | /* Test parsing functions */ | ||
| 21 | |||
| 22 | #include "support/mask_utils.h" | ||
| 23 | |||
| 24 | #include <stdbool.h> | ||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | #include <string.h> | ||
| 28 | #include <assert.h> | ||
| 29 | |||
| 30 | #define MAX_SIZE 512 | ||
| 31 | |||
| 32 | /* Test that the string 'str' is parsed to a cpu_set_t containing the bits in 'bits' */ | ||
| 33 | 21 | static void parse_and_check(const char *str, const int *bits) { | |
| 34 | int i; | ||
| 35 | 21 | int nelems = 0; | |
| 36 | 21 | bool error = false; | |
| 37 | cpu_set_t mask; | ||
| 38 | |||
| 39 | 21 | mu_parse_mask(str, &mask); | |
| 40 | |||
| 41 | // check every elem within MAX_SIZE | ||
| 42 |
3/4✓ Branch 0 taken 10752 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 10752 times.
✗ Branch 3 not taken.
|
10773 | for (i=0; i<MAX_SIZE && !error; ++i) { |
| 43 |
3/4✓ Branch 0 taken 10752 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 10703 times.
|
10752 | error = (bool)CPU_ISSET(i, &mask) != (bool)bits[i]; |
| 44 | 10752 | nelems += bits[i]; | |
| 45 | } | ||
| 46 | |||
| 47 | // check size | ||
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | error = error ? error : CPU_COUNT(&mask) != nelems; |
| 49 | |||
| 50 | 21 | fprintf(stderr, "String %s parsed as %s\n", str, mu_to_str(&mask)); | |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | assert( !error ); |
| 52 | 21 | } | |
| 53 | |||
| 54 | /* Test that the string 'str' is parsed to a cpu_set_t of such 'size' and contains 'cpuid' */ | ||
| 55 | 9 | static void parse_and_check2(const char *str, int size, int cpuid) { | |
| 56 | cpu_set_t mask; | ||
| 57 | 9 | mu_parse_mask(str, &mask); | |
| 58 | 9 | fprintf(stderr, "String %s parsed as %s\n", str, mu_to_str(&mask)); | |
| 59 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | assert( CPU_COUNT(&mask) == size ); |
| 60 |
3/6✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
|
9 | assert( CPU_ISSET(cpuid, &mask) ); |
| 61 | 9 | } | |
| 62 | |||
| 63 | 1 | int main(int argc, char *argv[]) { | |
| 64 | |||
| 65 | // Decimal | ||
| 66 | 1 | parse_and_check("0", (const int[MAX_SIZE]){1, 0, 0}); | |
| 67 | 1 | parse_and_check("1", (const int[MAX_SIZE]){0, 1, 0}); | |
| 68 | 1 | parse_and_check("0,2", (const int[MAX_SIZE]){1, 0, 1}); | |
| 69 | 1 | parse_and_check("10", (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}); | |
| 70 | // Ranges | ||
| 71 | 1 | parse_and_check("1,3-5,6,8", (const int[MAX_SIZE]){0, 1, 0, 1, 1, 1, 1, 0, 1}); | |
| 72 | 1 | parse_and_check("9-12", (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}); | |
| 73 | // Long numbers | ||
| 74 | 1 | parse_and_check2("63", 1, 63); | |
| 75 | 1 | parse_and_check2("64", 1, 64); | |
| 76 | 1 | parse_and_check2("500-511", 12, 511); | |
| 77 | |||
| 78 | // Binary | ||
| 79 | 1 | parse_and_check("0b1", (const int[MAX_SIZE]){1, 0, 0}); | |
| 80 | 1 | parse_and_check("0b10", (const int[MAX_SIZE]){0, 1, 0}); | |
| 81 | 1 | parse_and_check("0B101", (const int[MAX_SIZE]){1, 0, 1}); | |
| 82 | 1 | parse_and_check("0b10000000000", (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}); | |
| 83 | 1 | parse_and_check("0b00000000000000000000000000000000000000000000000000000000000101111010", | |
| 84 | 1 | (const int[MAX_SIZE]){0, 1, 0, 1, 1, 1, 1, 0, 1}); | |
| 85 | 1 | parse_and_check("0b1111000000000", | |
| 86 | 1 | (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}); | |
| 87 | 1 | parse_and_check2("0b1000000000000000000000000000000000000000000000000000000000000000", 1, 63); | |
| 88 | 1 | parse_and_check2("0b10000000000000000000000000000000000000000000000000000000000000000", 1, 64); | |
| 89 | 1 | parse_and_check2("0b" | |
| 90 | "1111111111110000000000000000000000000000000000000000000000000000" /* 64 bits */ | ||
| 91 | "0000000000000000000000000000000000000000000000000000000000000000" | ||
| 92 | "0000000000000000000000000000000000000000000000000000000000000000" | ||
| 93 | "0000000000000000000000000000000000000000000000000000000000000000" | ||
| 94 | "0000000000000000000000000000000000000000000000000000000000000000" | ||
| 95 | "0000000000000000000000000000000000000000000000000000000000000000" | ||
| 96 | "0000000000000000000000000000000000000000000000000000000000000000" | ||
| 97 | "0000000000000000000000000000000000000000000000000000000000000000", 12, 511); | ||
| 98 | |||
| 99 | // Hexadecimal | ||
| 100 | 1 | parse_and_check("0x1", (const int[MAX_SIZE]){1, 0, 0}); | |
| 101 | 1 | parse_and_check("0x2", (const int[MAX_SIZE]){0, 1, 0}); | |
| 102 | 1 | parse_and_check("0X5", (const int[MAX_SIZE]){1, 0, 1}); | |
| 103 | 1 | parse_and_check("0x400", (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}); | |
| 104 | 1 | parse_and_check("0x17A", (const int[MAX_SIZE]){0, 1, 0, 1, 1, 1, 1, 0, 1}); | |
| 105 | 1 | parse_and_check("0x00000000000000000000000000000000000000000000000000000000001E00", | |
| 106 | 1 | (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}); | |
| 107 | 1 | parse_and_check2("0x8000000000000000", 1, 63); | |
| 108 | 1 | parse_and_check2("0x10000000000000000", 1, 64); | |
| 109 | 1 | parse_and_check2("0x" | |
| 110 | "FFF0000000000000" /* 64 bits */ | ||
| 111 | "0000000000000000" | ||
| 112 | "0000000000000000" | ||
| 113 | "0000000000000000" | ||
| 114 | "0000000000000000" | ||
| 115 | "0000000000000000" | ||
| 116 | "0000000000000000" | ||
| 117 | "0000000000000000", 12, 511); | ||
| 118 | |||
| 119 | // Old binary | ||
| 120 | 1 | parse_and_check("101b", (const int[MAX_SIZE]){1, 0, 1}); | |
| 121 | 1 | parse_and_check("10b", (const int[MAX_SIZE]){1}); | |
| 122 | 1 | parse_and_check("00000000001b", (const int[MAX_SIZE]){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}); | |
| 123 | |||
| 124 | // Test string size limit | ||
| 125 | enum { OVERSIZED_STR_LEN = CPU_SETSIZE * 2 }; | ||
| 126 | char str[OVERSIZED_STR_LEN]; | ||
| 127 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 1 times.
|
2049 | for (int i=0; i<OVERSIZED_STR_LEN; ++i) str[i] = '1'; |
| 128 | cpu_set_t mask; | ||
| 129 | 1 | CPU_ZERO(&mask); | |
| 130 | 1 | mu_parse_mask(str, &mask); | |
| 131 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( CPU_COUNT(&mask) == 0 ); |
| 132 | |||
| 133 | /* mu_parse empty mask */ | ||
| 134 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | CPU_SET(1, &mask); |
| 135 | 1 | mu_parse_mask("", &mask); | |
| 136 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( CPU_COUNT(&mask) == 0 ); |
| 137 | |||
| 138 | /* mu_parse_to_slurm_format */ | ||
| 139 | 1 | mu_parse_mask("0-1,4,7,31,63,127-129", &mask); | |
| 140 | 1 | char *out_str = mu_parse_to_slurm_format(&mask); | |
| 141 | 1 | fprintf(stderr, "Slurm mask: %s\n", out_str); | |
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(strcmp(out_str, "0x380000000000000008000000080000093") == 0); |
| 143 | 1 | free(out_str); | |
| 144 | |||
| 145 | /* mu_equivalent_masks */ | ||
| 146 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( mu_equivalent_masks("42", "42") ); |
| 147 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( mu_equivalent_masks("0-7", "0-7") ); |
| 148 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( !mu_equivalent_masks("0-7", "0-3") ); |
| 149 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( !mu_equivalent_masks("0-7", "0") ); |
| 150 | |||
| 151 | /* None of the previous tests should initialize mask utils */ | ||
| 152 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( mu_testing_is_initialized() == false ); |
| 153 | |||
| 154 | 1 | return 0; | |
| 155 | } | ||
| 156 |