qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v2 2/6] qapi: convert StringOutputVisitor to use qem


From: Daniel P. Berrange
Subject: [Qemu-devel] [PATCH v2 2/6] qapi: convert StringOutputVisitor to use qemu_szutostr
Date: Fri, 9 Sep 2016 18:41:57 +0100

Instead inlining code for formatting sized integers,
call out to the new qemu_szutostr function.

Signed-off-by: Daniel P. Berrange <address@hidden>
---
 qapi/string-output-visitor.c       | 20 +++++---------------
 tests/test-string-output-visitor.c | 22 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/qapi/string-output-visitor.c b/qapi/string-output-visitor.c
index 94ac821..dfb15b5 100644
--- a/qapi/string-output-visitor.c
+++ b/qapi/string-output-visitor.c
@@ -15,6 +15,7 @@
 #include "qapi/string-output-visitor.h"
 #include "qapi/visitor-impl.h"
 #include "qemu/host-utils.h"
+#include "qemu/cutils.h"
 #include <math.h>
 #include "qemu/range.h"
 
@@ -211,10 +212,8 @@ static void print_type_size(Visitor *v, const char *name, 
uint64_t *obj,
                             Error **errp)
 {
     StringOutputVisitor *sov = to_sov(v);
-    static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
-    uint64_t div, val;
+    char *szval;
     char *out;
-    int i;
 
     if (!sov->human) {
         out = g_strdup_printf("%"PRIu64, *obj);
@@ -222,20 +221,11 @@ static void print_type_size(Visitor *v, const char *name, 
uint64_t *obj,
         return;
     }
 
-    val = *obj;
+    szval = qemu_szutostr_full(*obj, '\0', true, " ");
 
-    /* The exponent (returned in i) minus one gives us
-     * floor(log2(val * 1024 / 1000).  The correction makes us
-     * switch to the higher power when the integer part is >= 1000.
-     */
-    frexp(val / (1000.0 / 1024.0), &i);
-    i = (i - 1) / 10;
-    assert(i < ARRAY_SIZE(suffixes));
-    div = 1ULL << (i * 10);
-
-    out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
-                          (double)val/div, suffixes[i], i ? "iB" : "");
+    out = g_strdup_printf("%"PRIu64" (%s)", *obj, szval);
     string_output_set(sov, out);
+    g_free(szval);
 }
 
 static void print_type_bool(Visitor *v, const char *name, bool *obj,
diff --git a/tests/test-string-output-visitor.c 
b/tests/test-string-output-visitor.c
index 444844a..dd6e00f 100644
--- a/tests/test-string-output-visitor.c
+++ b/tests/test-string-output-visitor.c
@@ -87,6 +87,24 @@ static void test_visitor_out_int(TestOutputVisitorData *data,
     }
 }
 
+static void test_visitor_out_size(TestOutputVisitorData *data,
+                                  const void *unused)
+{
+    uint64_t value = 1729;
+    Error *err = NULL;
+    char *str;
+
+    visit_type_size(data->ov, NULL, &value, &err);
+    g_assert(!err);
+
+    str = visitor_get(data);
+    if (data->human) {
+        g_assert_cmpstr(str, ==, "1729 (1.69 KiB)");
+    } else {
+        g_assert_cmpstr(str, ==, "1729");
+    }
+}
+
 static void test_visitor_out_intList(TestOutputVisitorData *data,
                                      const void *unused)
 {
@@ -240,6 +258,10 @@ int main(int argc, char **argv)
                             &out_visitor_data, test_visitor_out_int, false);
     output_visitor_test_add("/string-visitor/output/int-human",
                             &out_visitor_data, test_visitor_out_int, true);
+    output_visitor_test_add("/string-visitor/output/size",
+                            &out_visitor_data, test_visitor_out_size, false);
+    output_visitor_test_add("/string-visitor/output/size-human",
+                            &out_visitor_data, test_visitor_out_size, true);
     output_visitor_test_add("/string-visitor/output/bool",
                             &out_visitor_data, test_visitor_out_bool, false);
     output_visitor_test_add("/string-visitor/output/bool-human",
-- 
2.7.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]