You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.2 KiB

  1. const chai = require("chai");
  2. const assert = chai.assert;
  3. const fs = require("fs");
  4. var tmp = require("tmp-promise");
  5. const path = require("path");
  6. const util = require("util");
  7. const exec = util.promisify(require("child_process").exec);
  8. const BuildZqField = require("./buildzqfield");
  9. module.exports = testField;
  10. async function testField(prime, test) {
  11. tmp.setGracefulCleanup();
  12. const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
  13. const source = await BuildZqField(prime, "Fr");
  14. // console.log(dir.path);
  15. await fs.promises.writeFile(path.join(dir.path, "fr.asm"), source.asm, "utf8");
  16. await fs.promises.writeFile(path.join(dir.path, "fr.h"), source.h, "utf8");
  17. await fs.promises.writeFile(path.join(dir.path, "fr.c"), source.c, "utf8");
  18. await exec(`cp ${path.join(__dirname, "tester.cpp")} ${dir.path}`);
  19. await exec("nasm -fmacho64 --prefix _ " +
  20. ` ${path.join(dir.path, "fr.asm")}`
  21. );
  22. await exec("g++" +
  23. ` ${path.join(dir.path, "tester.cpp")}` +
  24. ` ${path.join(dir.path, "fr.o")}` +
  25. ` ${path.join(dir.path, "fr.c")}` +
  26. ` -o ${path.join(dir.path, "tester")}` +
  27. " -lgmp"
  28. );
  29. const inLines = [];
  30. for (let i=0; i<test.length; i++) {
  31. for (let j=0; j<test[i][0].length; j++) {
  32. inLines.push(test[i][0][j]);
  33. }
  34. }
  35. inLines.push("");
  36. await fs.promises.writeFile(path.join(dir.path, "in.tst"), inLines.join("\n"), "utf8");
  37. await exec(`${path.join(dir.path, "tester")}` +
  38. ` <${path.join(dir.path, "in.tst")}` +
  39. ` >${path.join(dir.path, "out.tst")}`);
  40. const res = await fs.promises.readFile(path.join(dir.path, "out.tst"), "utf8");
  41. const resLines = res.split("\n");
  42. for (let i=0; i<test.length; i++) {
  43. const expected = test[i][1].toString();
  44. const calculated = resLines[i];
  45. if (calculated != expected) {
  46. console.log("FAILED");
  47. for (let j=0; j<test[i][0].length; j++) {
  48. console.log(test[i][0][j]);
  49. }
  50. console.log("Should Return: " + expected);
  51. console.log("But Returns: " + calculated);
  52. }
  53. assert.equal(calculated, expected);
  54. }
  55. }