MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / TestC

Class TestC

chapter21/src/test/java/com/seaofnodes/simple/TestC.java:12–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10// Call a C driver program, which calls a Simple-generated .o, and self-checks.
11// Expects GCC from the default CLI, compiles C, compiles Simple, links and runs.
12public abstract class TestC {
13
14 public static final String OS = System.getProperty("os.name");
15 public static final String CPU = System.getProperty("os.arch");
16
17 public static final String CALL_CONVENTION = OS.startsWith("Windows") ? "Win64" : "SystemV";
18 public static final String CPU_PORT = switch( CPU ) {
19 case "amd64" -> "x86_64_v2";
20 default -> throw Utils.TODO("Map Yer CPU Port Here");
21 };
22
23 public static void run( String file, int spills ) throws IOException { run(file,"",spills); }
24
25 public static void run( String file, String expected, int spills ) throws IOException {
26 run("src/test/java/com/seaofnodes/simple/progs",file,expected,spills);
27 }
28
29 // Compile and run a simple program
30 public static void run( String dir, String file, String expected, int spills ) throws IOException {
31 // Files
32 String cfile = dir+"/"+file+".c" ;
33 String sfile = dir+"/"+file+".smp";
34 String efile = "build/objs/"+file;
35
36 // Compile and export Simple
37 String src = Files.readString(Path.of(sfile));
38 _run(src,CALL_CONVENTION,"",cfile,efile,"S",expected,spills);
39 }
40
41 static void _run( String src, String simple_conv, String c_conv, String cfile, String efile, String xtn, String expected, int spills ) throws IOException {
42 String bin = efile+xtn;
43 String obj = bin+".o";
44 // Compile simple, emit ELF
45 CodeGen code = new CodeGen(src).driver( CPU_PORT, simple_conv, obj);
46
47 // Compile the C program
48 var params = new String[] {
49 //if (USE_WSL) "wsl.exe";
50 "gcc",
51 cfile,
52 obj,
53 "-lm", // Picks up 'sqrt' for newtonFloat tests to compare
54 "-g",
55 "-o",
56 bin,
57 "-D",
58 "CALL_CONV="+c_conv,
59 };
60 Process gcc = new ProcessBuilder(params).redirectErrorStream(true).start();
61 byte error;
62 try { error = (byte)gcc.waitFor(); } catch( InterruptedException e ) { throw new IOException("interrupted"); }
63 String result = new String(gcc.getInputStream().readAllBytes());
64 if( error!=0 ) {
65 System.err.println("gcc error code: "+error);
66 System.err.println(result);
67 }
68 assertEquals( 0, error );
69 //assertTrue(result.isEmpty()); // No data in error stream

Callers

nothing calls this directly

Calls 1

TODOMethod · 0.95

Tested by

no test coverage detected