cpu-set-util: use %d-%d format in cpu_set_to_range_string() only for actual ranges
authorMichal Sekletar <msekleta@redhat.com>
Mon, 3 Jun 2019 08:12:35 +0000 (10:12 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 3 Jun 2019 20:36:58 +0000 (22:36 +0200)
src/shared/cpu-set-util.c
src/test/test-cpu-set-util.c

index 7c8f4d42d279a28455ca47561b1814391afbdf2d..b0036c7f619bea6736e6496265413e25e392ec1e 100644 (file)
@@ -55,7 +55,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
                         if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
                                 return NULL;
 
-                        if (range_end > range_start || len == 0)
+                        if (range_end > range_start)
                                 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
                         else
                                 r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
@@ -67,7 +67,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
                 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
                         return NULL;
 
-                if (range_end > range_start || len == 0)
+                if (range_end > range_start)
                         r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
                 else
                         r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
index cb970a61c153f7cc14cda07c4b92750381ea3caa..90f5755957de34528c265ffcfbf31853abe9999a 100644 (file)
@@ -24,7 +24,7 @@ static void test_parse_cpu_set(void) {
         str = mfree(str);
         assert_se(str = cpu_set_to_range_string(&c));
         log_info("cpu_set_to_range_string: %s", str);
-        assert_se(streq(str, "0-0"));
+        assert_se(streq(str, "0"));
         str = mfree(str);
         cpu_set_reset(&c);
 
@@ -93,17 +93,19 @@ static void test_parse_cpu_set(void) {
         cpu_set_reset(&c);
 
         /* Commas with spaces (and trailing comma, space) */
-        assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
+        assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
-        assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
+        assert_se(CPU_COUNT_S(c.allocated, c.set) == 9);
         for (cpu = 0; cpu < 8; cpu++)
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
+
+        assert_se(CPU_ISSET_S(63, c.allocated, c.set));
         assert_se(str = cpu_set_to_string(&c));
         log_info("cpu_set_to_string: %s", str);
         str = mfree(str);
         assert_se(str = cpu_set_to_range_string(&c));
         log_info("cpu_set_to_range_string: %s", str);
-        assert_se(streq(str, "0-7"));
+        assert_se(streq(str, "0-7 63"));
         str = mfree(str);
         cpu_set_reset(&c);
 
@@ -159,11 +161,11 @@ static void test_parse_cpu_set(void) {
         cpu_set_reset(&c);
 
         /* Mix ranges and individual CPUs */
-        assert_se(parse_cpu_set_full("0,1 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
+        assert_se(parse_cpu_set_full("0,2 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
         assert_se(CPU_COUNT_S(c.allocated, c.set) == 10);
         assert_se(CPU_ISSET_S(0, c.allocated, c.set));
-        assert_se(CPU_ISSET_S(1, c.allocated, c.set));
+        assert_se(CPU_ISSET_S(2, c.allocated, c.set));
         for (cpu = 4; cpu < 12; cpu++)
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
         assert_se(str = cpu_set_to_string(&c));
@@ -171,7 +173,7 @@ static void test_parse_cpu_set(void) {
         str = mfree(str);
         assert_se(str = cpu_set_to_range_string(&c));
         log_info("cpu_set_to_range_string: %s", str);
-        assert_se(streq(str, "0-1 4-11"));
+        assert_se(streq(str, "0 2 4-11"));
         str = mfree(str);
         cpu_set_reset(&c);