diff options
author | Ian Campbell <ian.campbell@citrix.com> | 2011-06-21 18:26:39 +0100 |
---|---|---|
committer | Ian Campbell <ian.campbell@citrix.com> | 2011-06-21 18:26:39 +0100 |
commit | a53cb5b82d0490241ec48bd0083939823b6d39f9 (patch) | |
tree | 92c484146f5c7d0c9c3ba435ecb579d380bb713e /tools/libxl/gentest.py | |
parent | dbf20f55f0c154d11153c40f2a709b8c32bb83cc (diff) | |
download | xen-a53cb5b82d0490241ec48bd0083939823b6d39f9.tar.gz xen-a53cb5b82d0490241ec48bd0083939823b6d39f9.tar.bz2 xen-a53cb5b82d0490241ec48bd0083939823b6d39f9.zip |
libxl: autogenerate to_string and from_string functions for Enumerations.
The generated strings are the lower case enum value names, with underscores.
Accepted string for parsing are the same but are case insensitive.
We provide a table of strings->value for each Enumeration as well as a
convenience function to perform a lookup.
Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
Diffstat (limited to 'tools/libxl/gentest.py')
-rw-r--r-- | tools/libxl/gentest.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tools/libxl/gentest.py b/tools/libxl/gentest.py new file mode 100644 index 0000000000..5828cfd17a --- /dev/null +++ b/tools/libxl/gentest.py @@ -0,0 +1,63 @@ +#!/usr/bin/python + +import sys +import re +import random + +import libxltypes +def randomize_char(c): + if random.random() < 0.5: + return str.lower(c) + else: + return str.upper(c) + +def randomize_case(s): + r = [randomize_char(c) for c in s] + return "".join(r) + +if __name__ == '__main__': + if len(sys.argv) < 3: + print >>sys.stderr, "Usage: gentest.py <idl> <implementation>" + sys.exit(1) + + random.seed() + + idl = sys.argv[1] + (_,types) = libxltypes.parse(idl) + + impl = sys.argv[2] + f = open(impl, "w") + f.write(""" +#include <stdio.h> +#include \"libxl.h\" + +int main(int argc, char **argv) +{ +""") + + for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]: + f.write(" %s %s_val;\n" % (ty.typename, ty.typename)) + f.write(" int rc;\n") + f.write("\n") + + for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]: + f.write(" printf(\"%s -- to string:\\n\");\n" % (ty.typename)) + for v in ty.values: + f.write(" printf(\"\\t%s = %%d = \\\"%%s\\\"\\n\", %s, %s_to_string(%s));\n" %\ + (v.valuename, v.name, ty.typename, v.name)) + f.write("\n") + + f.write(" printf(\"%s -- from string:\\n\");\n" % (ty.typename)) + for v in [v.valuename for v in ty.values] + ["AN INVALID VALUE"]: + n = randomize_case(v) + f.write(" %s_val = -1;\n" % (ty.typename)) + f.write(" rc = %s_from_string(\"%s\", &%s_val);\n" %\ + (ty.typename, n, ty.typename)) + + f.write(" printf(\"\\t%s = \\\"%%s\\\" = %%d (rc %%d)\\n\", \"%s\", %s_val, rc);\n" %\ + (v, n, ty.typename)) + f.write("\n") + + f.write("""return 0; +} +""") |