From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Gibson Subject: Re: [kvm-unit-tests PATCH v4 06/17] lib: share arm-selftest utility functions Date: Wed, 17 Feb 2016 11:14:36 +1100 Message-ID: <20160217001436.GR2269@voom.redhat.com> References: <1455544166-19766-1-git-send-email-drjones@redhat.com> <1455544166-19766-7-git-send-email-drjones@redhat.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="VmyrZ50r30oK77nV" Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, thuth@redhat.com, dgibson@redhat.com, agraf@suse.de, lvivier@redhat.com, pbonzini@redhat.com To: Andrew Jones Return-path: Received: from ozlabs.org ([103.22.144.67]:40573 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932904AbcBQBPM (ORCPT ); Tue, 16 Feb 2016 20:15:12 -0500 Content-Disposition: inline In-Reply-To: <1455544166-19766-7-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: --VmyrZ50r30oK77nV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Feb 15, 2016 at 02:49:15PM +0100, Andrew Jones wrote: > arm-selftest has a couple utility functions that could be useful > to other unit tests, even other architectures. So move them out. > split_var moves to lib/util, where we can add other random > utilities over time. assert_args inspires report_abort, which > allows us to report a message, using the current prefix, that > we're aborting (outputs ABORT vs. PASS/FAIL). This is useful > for cases when unit tests can't complete due to missing > dependencies of some sort, such as missing/invalid inputs from > the user. >=20 > Signed-off-by: Andrew Jones > Reviewed-by: Thomas Huth > Tested-by: Laurent Vivier > --- > arm/selftest.c | 45 ++++++++++++++------------------------= ------ > config/config-arm-common.mak | 1 + > lib/libcflat.h | 11 ++++++----- > lib/report.c | 16 ++++++++++++++++ > lib/util.c | 18 ++++++++++++++++++ > lib/util.h | 23 ++++++++++++++++++++++ > 6 files changed, 78 insertions(+), 36 deletions(-) > create mode 100644 lib/util.c > create mode 100644 lib/util.h >=20 > diff --git a/arm/selftest.c b/arm/selftest.c > index aad7eecd529ad..75dc91faab69a 100644 > --- a/arm/selftest.c > +++ b/arm/selftest.c > @@ -6,6 +6,7 @@ > * This work is licensed under the terms of the GNU LGPL, version 2. > */ > #include > +#include > #include > #include > #include > @@ -18,43 +19,21 @@ > #include > #include > =20 > -static void assert_args(int num_args, int needed_args) > -{ > - if (num_args < needed_args) { > - printf("selftest: not enough arguments\n"); > - abort(); > - } > -} > - > -static char *split_var(char *s, long *val) > -{ > - char *p; > - > - p =3D strchr(s, '=3D'); > - if (!p) > - return NULL; > - > - *val =3D atol(p+1); > - *p =3D '\0'; > - > - return s; > -} > - > static void check_setup(int argc, char **argv) > { > - int nr_tests =3D 0, i; > - char *var; > + int nr_tests =3D 0, len, i; > long val; > =20 > for (i =3D 0; i < argc; ++i) { > =20 > - var =3D split_var(argv[i], &val); > - if (!var) > + len =3D parse_keyval(argv[i], &val); > + if (len =3D=3D -1) > continue; > =20 > - report_prefix_push(var); > + argv[i][len] =3D '\0'; > + report_prefix_push(argv[i]); > =20 > - if (strcmp(var, "mem") =3D=3D 0) { > + if (strcmp(argv[i], "mem") =3D=3D 0) { > =20 > phys_addr_t memsize =3D PHYS_END - PHYS_OFFSET; > phys_addr_t expected =3D ((phys_addr_t)val)*1024*1024; > @@ -63,7 +42,7 @@ static void check_setup(int argc, char **argv) > memsize/1024/1024); > ++nr_tests; > =20 > - } else if (strcmp(var, "smp") =3D=3D 0) { > + } else if (strcmp(argv[i], "smp") =3D=3D 0) { > =20 > report("nr_cpus =3D %d", nr_cpus =3D=3D (int)val, nr_cpus); > ++nr_tests; > @@ -72,7 +51,8 @@ static void check_setup(int argc, char **argv) > report_prefix_pop(); > } > =20 > - assert_args(nr_tests, 2); > + if (nr_tests < 2) > + report_abort("missing input"); > } > =20 > static struct pt_regs expected_regs; > @@ -343,7 +323,10 @@ static void cpu_report(void) > int main(int argc, char **argv) > { > report_prefix_push("selftest"); > - assert_args(argc, 1); > + > + if (!argc) > + report_abort("no test specified"); > + > report_prefix_push(argv[0]); > =20 > if (strcmp(argv[0], "setup") =3D=3D 0) { > diff --git a/config/config-arm-common.mak b/config/config-arm-common.mak > index 698555d6a676f..bd153cf6ea5ba 100644 > --- a/config/config-arm-common.mak > +++ b/config/config-arm-common.mak > @@ -27,6 +27,7 @@ CFLAGS +=3D -I lib -I lib/libfdt > asm-offsets =3D lib/$(ARCH)/asm-offsets.h > include config/asm-offsets.mak > =20 > +cflatobjs +=3D lib/util.o > cflatobjs +=3D lib/alloc.o > cflatobjs +=3D lib/devicetree.o > cflatobjs +=3D lib/virtio.o > diff --git a/lib/libcflat.h b/lib/libcflat.h > index 9747ccdbc9f1d..8411f6c5d92e3 100644 > --- a/lib/libcflat.h > +++ b/lib/libcflat.h > @@ -57,11 +57,12 @@ extern int snprintf(char *buf, int size, const char *= fmt, ...); > extern int vsnprintf(char *buf, int size, const char *fmt, va_list va); > extern long atol(const char *ptr); > =20 > -void report_prefix_push(const char *prefix); > -void report_prefix_pop(void); > -void report(const char *msg_fmt, bool pass, ...); > -void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...); > -int report_summary(void); > +extern void report_prefix_push(const char *prefix); > +extern void report_prefix_pop(void); > +extern void report(const char *msg_fmt, bool pass, ...); > +extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...= ); > +extern void report_abort(const char *msg_fmt, ...); > +extern int report_summary(void); > =20 > #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0])) > =20 > diff --git a/lib/report.c b/lib/report.c > index 35e664108a921..62916c4ac3c8a 100644 > --- a/lib/report.c > +++ b/lib/report.c > @@ -96,3 +96,19 @@ int report_summary(void) > =20 > spin_unlock(&lock); > } > + > +void report_abort(const char *msg_fmt, ...) > +{ > + va_list va; > + char buf[2000]; > + > + puts("ABORT: "); > + puts(prefixes); > + va_start(va, msg_fmt); > + vsnprintf(buf, sizeof(buf), msg_fmt, va); Is there a reason for using vsnprintf() rather than just vprintf() here? Apart from that, Reviewed-by: David Gibson > + va_end(va); > + puts(buf); > + puts("\n"); > + report_summary(); > + abort(); > +} > diff --git a/lib/util.c b/lib/util.c > new file mode 100644 > index 0000000000000..69b18100c9722 > --- /dev/null > +++ b/lib/util.c > @@ -0,0 +1,18 @@ > +/* > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones > + * > + * This work is licensed under the terms of the GNU LGPL, version 2. > + */ > +#include > + > +int parse_keyval(char *s, long *val) > +{ > + char *p; > + > + p =3D strchr(s, '=3D'); > + if (!p) > + return -1; > + > + *val =3D atol(p+1); > + return p - s; > +} > diff --git a/lib/util.h b/lib/util.h > new file mode 100644 > index 0000000000000..4c4b441322770 > --- /dev/null > +++ b/lib/util.h > @@ -0,0 +1,23 @@ > +#ifndef _UTIL_H_ > +#define _UTIL_H_ > +/* > + * Collection of utility functions to share between unit tests. > + * > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones > + * > + * This work is licensed under the terms of the GNU LGPL, version 2. > + */ > + > +/* > + * parse_keyval extracts the integer from a string formatted as > + * string=3Dinteger. This is useful for passing expected values to > + * the unit test on the command line, i.e. it helps parse QEMU > + * command lines that include something like -append var1=3D1 var2=3D2 > + * @s is the input string, likely a command line parameter, and > + * @val is a pointer to where the integer will be stored. > + * > + * Returns the offset of the '=3D', or -1 if no keyval pair is found. > + */ > +extern int parse_keyval(char *s, long *val); > + > +#endif --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --VmyrZ50r30oK77nV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJWw7tsAAoJEGw4ysog2bOSJo4P/jgH/ty9V04CLHFyNb+8Rbv+ dVsEQd0NjxslfvPlwUvWq6Kp0Ete8Kn8tyJBAq3fAwtEt8/piO1UHUKjVUcBEagC RLR7OaTGJQLbX3JEExR3YZGUximTfgAeN+SuauPrb6nvutcPDAHu+8Fi/jb5dREl XnR3sUXLFYLVq6nih73DkHY6+1n4GPyAs4C0ztnM0n7Tt0g7AAX6wIbbF+brHdGJ S4TfWDrJFlbSg5A7zPURwiGk+x2iNWd2gKY3GE/8YdHtdpk5xcMaLlhCSbwLq9bu /Npa99m9GAmBxFRBkb14BJmyr9abcjJVQopepmi8A3S36Rk4KCOH1BM0S0FD4yeb wASPsAwbG8+EILrPwU4bW8Rtjq9F6ghO/amd2gjrEqUP/+GFc+5y7F2PG+FWy57v 7CAdwrKEeadEKJfITdnSKwz/b144yQIi9kxgyba6tU4EucMx8fh9h/d73qRAV0b9 5amVv7DNQtnm6nqcrxgjQPhObUZfUvddY3griX7dzvzdylfyUFyKKU37o/OeyhBt kcfyAiVlxFwO9cMBBOoyL6FXsWnblJ/a+AmSthjIUHK79ZlernEZrJuMr3D8i3Jn IiwN8mcA9o0li8mSnTLkp9SWZCjf/ggwaY5949X4r8dfLdgD7TTGcoorAg4lroju D0gkGmfnzxEt808WaGRw =9AQ0 -----END PGP SIGNATURE----- --VmyrZ50r30oK77nV--