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.

108 lines
2.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "regexp"
  9. "strings"
  10. "github.com/kylelemons/godebug/diff"
  11. )
  12. func fileExists(f string) bool {
  13. _, err := os.Stat(f);
  14. return err == nil;
  15. }
  16. var inputre = regexp.MustCompile(`\.input$`)
  17. type CPTest struct {
  18. Name string
  19. Input *[]byte
  20. Output *[]byte
  21. }
  22. func runOneTest(dir string, test *CPTest) {
  23. fmt.Println("Running Test", test.Name, "...");
  24. fmt.Println("==========");
  25. fmt.Println("INPUT:")
  26. fmt.Println(string(*test.Input));
  27. fmt.Println("==========");
  28. cmd := exec.Command("go", "run", path.Join(dir, "main.go"));
  29. inpipe, _ := cmd.StdinPipe();
  30. defer inpipe.Close();
  31. fmt.Fprintln(inpipe, string(*test.Input));
  32. out,err := cmd.CombinedOutput();
  33. fmt.Println("OUTPUT:")
  34. if err != nil {
  35. fmt.Println(err);
  36. fmt.Println(string(out));
  37. return
  38. }
  39. fmt.Println(string(out));
  40. if test.Output != nil {
  41. fmt.Println("==========");
  42. fmt.Println("ANSWER:")
  43. fmt.Println(string(*test.Output));
  44. fmt.Println("==========");
  45. fmt.Println("DIFF:")
  46. fmt.Println(diff.Diff(string(out), string(*test.Output)))
  47. }
  48. fmt.Println("==========");
  49. }
  50. func runTests(dir string, selected []string) {
  51. testfolder := path.Join(dir, "cpgo_tests")
  52. info, err := os.Stat(testfolder);
  53. if err != nil {
  54. fmt.Println("Error encountered", err);
  55. os.Exit(1);
  56. }
  57. if !info.IsDir() {
  58. fmt.Println(testfolder, "is not a directory.")
  59. os.Exit(1);
  60. }
  61. entries, err := ioutil.ReadDir(testfolder); if err != nil {
  62. fmt.Println("Error while accessing", testfolder, err);
  63. os.Exit(1);
  64. }
  65. tests := make([]CPTest, 0);
  66. testmap := map[string]*CPTest{};
  67. for _,file := range entries {
  68. if strings.HasSuffix(file.Name(), ".input") {
  69. fpath := path.Join(testfolder, file.Name());
  70. input,_ := ioutil.ReadFile(fpath);
  71. outputfile := string(inputre.ReplaceAll([]byte(fpath), []byte(".output")));
  72. tst := CPTest{
  73. Name: strings.TrimSuffix(file.Name(), ".input"),
  74. Input: &input,
  75. };
  76. if fileExists(outputfile) {
  77. out,_ := ioutil.ReadFile(outputfile);
  78. tst.Output = &out;
  79. }
  80. tests = append(tests, tst);
  81. testmap[tst.Name] = &tst;
  82. }
  83. }
  84. if len(selected) == 0 {
  85. for _, t := range tests {
  86. runOneTest(dir, &t);
  87. }
  88. } else {
  89. for _,name := range selected {
  90. t, exists := testmap[name];
  91. if exists {
  92. runOneTest(dir, t);
  93. } else {
  94. fmt.Println(name, "is not a valid test.");
  95. return
  96. }
  97. }
  98. }
  99. }