/* -*-c-*-
*
- * $Id: group-test.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
+ * $Id: group-test.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
*
* Testing group operations
*
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: group-test.c,v $
- * Revision 1.1 2004/04/01 12:50:09 mdw
- * Add cyclic group abstraction, with test code. Separate off exponentation
- * functions for better static linking. Fix a buttload of bugs on the way.
- * Generally ensure that negative exponents do inversion correctly. Add
- * table of standard prime-field subgroups. (Binary field subgroups are
- * currently unimplemented but easy to add if anyone ever finds a good one.)
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <stdarg.h>
return (ok);
}
+static int vtoraw(dstr *v)
+{
+ group *g = getgroup(v[0].buf);
+ ge *x = getge(g, v[1].buf);
+ int ir = *(int *)v[2].buf;
+ dstr c = DSTR_INIT;
+ int ic;
+ buf b;
+ int ok = 1;
+
+ dstr_ensure(&c, v[3].len);
+ buf_init(&b, c.buf, v[3].len);
+ ic = G_TORAW(g, &b, x);
+ c.len = BLEN(&b);
+ if (ic != ir || (!ic && (c.len != v[3].len ||
+ memcmp(c.buf, v[3].buf, c.len)))) {
+ ok = 0;
+ fprintf(stderr, "*** toraw failed\n");
+ fprintf(stderr, "*** group: %s\n", v[0].buf);
+ show(g, "x", x);
+ if (ir) fprintf(stderr, "*** expected failure\n");
+ else {
+ fprintf(stderr, "*** expected: "); type_hex.dump(&v[3], stderr);
+ fprintf(stderr, "\n*** computed: "); type_hex.dump(&c, stderr);
+ fputc('\n', stderr);
+ }
+ }
+ G_DESTROY(g, x); dstr_destroy(&c);
+ G_DESTROYGROUP(g);
+ assert(mparena_count(MPARENA_GLOBAL) == 0);
+ return (ok);
+}
+
+static int vfromraw(dstr *v)
+{
+ group *g = getgroup(v[0].buf);
+ int ir = *(int *)v[2].buf;
+ ge *r = getge(g, v[3].buf);
+ int ic;
+ ge *c = G_CREATE(g);
+ buf b;
+ int ok = 1;
+
+ buf_init(&b, v[1].buf, v[1].len);
+ ic = G_FROMRAW(g, &b, c);
+ if ((ic < 0) != (ir < 0) || (ir >= 0 &&
+ (ir != BLEN(&b) || !G_EQ(g, r, c)))) {
+ ok = 0;
+ fprintf(stderr, "*** fromraw failed\n");
+ fprintf(stderr, "*** group: %s\n", v[0].buf);
+ fprintf(stderr, "*** input string: "); type_hex.dump(&v[1], stderr);
+ fputc('\n', stderr);
+ if (ir < 0) fprintf(stderr, "*** expected failure\n");
+ else {
+ show(g, "expected", r); show(g, "computed", c);
+ fprintf(stderr, "*** expected used = %d\n", ir);
+ fprintf(stderr, "*** computed used = %lu\n", (unsigned long)BLEN(&b));
+ }
+ }
+ G_DESTROY(g, r); G_DESTROY(g, c);
+ G_DESTROYGROUP(g);
+ assert(mparena_count(MPARENA_GLOBAL) == 0);
+ return (ok);
+}
+
static const test_chunk tests[] = {
{ "check", vcheck, { &type_string, &type_string } },
{ "checkelt", vcheckelt, { &type_string, &type_string, &type_int } },
&type_int, &type_hex } },
{ "frombuf", vfrombuf, { &type_string, &type_hex,
&type_int, &type_string } },
+ { "toraw", vtoraw, { &type_string, &type_string,
+ &type_int, &type_hex } },
+ { "fromraw", vfromraw, { &type_string, &type_hex,
+ &type_int, &type_string } },
{ 0 }
};