summaryrefslogtreecommitdiff
path: root/chapter21/src/test
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-03-23 10:01:20 -0700
committerCliff Click <cliffc@acm.org>2025-03-23 10:01:20 -0700
commitd333a9001c4f1db159dca8395b5e59c2d2aa92d7 (patch)
tree99b1386db962cba1a4c047d07fd3654d9338703e /chapter21/src/test
parent71d882bae6c88ce20353b787cb3699860d52b3d5 (diff)
downloadSimple-d333a9001c4f1db159dca8395b5e59c2d2aa92d7.tar.gz
Simple-d333a9001c4f1db159dca8395b5e59c2d2aa92d7.zip
Share a bunch of larger common tests
Move them to `*.smp` files
Diffstat (limited to 'chapter21/src/test')
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java40
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter19Test.java93
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java97
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java45
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/progs/array1.smp9
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/progs/fcn2.smp5
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/progs/fcn3.smp42
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp10
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp37
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/progs/stringHash.smp36
10 files changed, 133 insertions, 281 deletions
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index 7dd0710a..e011392d 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter15Test.java
@@ -1,6 +1,9 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -210,41 +213,8 @@ return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
}
@Test
- public void sieveOEratosthenes() {
- CodeGen code = new CodeGen(
-"""
-int[] !ary = new int[arg], !primes = new int[arg];
-int nprimes = 0, p=2;
-// Find primes while p^2 < arg
-while( p*p < arg ) {
- // skip marked non-primes
- while( ary[p]==1 ) p = p + 1;
- // p is now a prime
- primes[nprimes] = p; nprimes = nprimes + 1;
- // Mark out the rest non-primes
- int i = p + p;
- while( i < ary# ) {
- ary[i] = 1;
- i = i + p;
- }
- p = p + 1;
-}
-// Now just collect the remaining primes, no more marking
-while( p < arg ) {
- if( ary[p] == 0 ) {
- primes[nprimes] = p; nprimes = nprimes + 1;
- }
- p = p + 1;
-}
-// Copy/shrink the result array
-int[] !rez = new int[nprimes];
-int j = 0;
-while( j < nprimes ) {
- rez[j] = primes[j];
- j = j + 1;
-}
-return rez;
-""");
+ public void sieveOEratosthenes() throws IOException {
+ CodeGen code = new CodeGen(Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/sieve.smp")));
code.parse().opto();
assertEquals("return [int];", code.print());
assertEquals("int[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20));
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter19Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter19Test.java
index 7bef13f5..9d228f54 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter19Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter19Test.java
@@ -1,6 +1,9 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import org.junit.Ignore;
import org.junit.Test;
import static com.seaofnodes.simple.Main.PORTS;
@@ -21,44 +24,9 @@ return 0;
@Test
- public void testString() {
- CodeGen code = new CodeGen(
-"""
-struct String {
- u8[] cs;
- int _hashCode;
-};
-
-val equals = { String self, String s ->
- if( self == s ) return true;
- if( self.cs# != s.cs# ) return false;
- for( int i=0; i< self.cs#; i++ )
- if( self.cs[i] != s.cs[i] )
- return false;
- return true;
-};
-
-val hashCode = { String self ->
- self._hashCode
- ? self._hashCode
- : (self._hashCode = _hashCodeString(self));
-};
-
-val _hashCodeString = { String self ->
- int hash=0;
- if( self.cs ) {
- for( int i=0; i< self.cs#; i++ )
- hash = hash*31 + self.cs[i];
- }
- if( !hash ) hash = 123456789;
- return hash;
-};
-
-String !s = new String { cs = new u8[17]; };
-s.cs[0] = 67; // C
-s.cs[1] = 108; // l
-hashCode(s)+hashCode(s);""");
- code.parse().opto().typeCheck().GCM().localSched();
+ public void testString() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
+ CodeGen code = new CodeGen(src).parse().opto().typeCheck().GCM().localSched();
assertEquals("Stop[ return (#2+#2); return Phi(Region,1,0,0,1); return Phi(Region,._hashCode,Phi(Region,123456789,Phi(Loop,0,(.[]+((Phi_hash<<5)-Phi_hash))))); ]", code._stop.toString());
assertEquals("-4898613127354160978", Eval2.eval(code, 2));
}
@@ -324,53 +292,18 @@ return A[1];
}
@Test
- public void testNewton() {
- CodeGen code = new CodeGen(
-"""
-// Newtons approximation to the square root
-val sqrt = { flt x ->
- flt guess = x;
- while( 1 ) {
- flt next = (x/guess + guess)/2;
- if( next == guess ) return guess;
- guess = next;
- }
-};
-flt farg = arg;
-return sqrt(farg);
-""");
- code.parse().opto().typeCheck().instSelect(PORTS,"x86_64_v2", "SystemV").GCM().localSched();
+ public void testNewton() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp"))
+ + "flt farg = arg; return sqrt(farg);";
+ CodeGen code = new CodeGen(src).parse().opto().typeCheck().instSelect(PORTS,"x86_64_v2", "SystemV").GCM().localSched();
assertEquals("return Phi(Loop,(cvtf,arg),(mulf,(addf,(divf,cvtf,Phi_guess),Phi_guess),0.5f));", code.print());
};
@Test
- public void sieveOfEratosthenes() {
- CodeGen code = new CodeGen(
-"""
-var ary = new bool[arg], primes = new int[arg];
-var nprimes=0, p=0;
-// Find primes while p^2 < arg
-for( p=2; p*p < arg; p++ ) {
- // skip marked non-primes
- while( ary[p] ) p++;
- // p is now a prime
- primes[nprimes++] = p;
- // Mark out the rest non-primes
- for( int i = p + p; i < ary#; i += p )
- ary[i] = true;
-}
-// Now just collect the remaining primes, no more marking
-for( ; p < arg; p++ )
- if( !ary[p] )
- primes[nprimes++] = p;
-// Copy/shrink the result array
-var !rez = new int[nprimes];
-for( int j=0; j<nprimes; j++ )
- rez[j] = primes[j];
-return rez;
-""");
- code.parse().opto().typeCheck().instSelect(PORTS,"x86_64_v2", "SystemV").GCM().localSched();
+ public void sieveOfEratosthenes() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/sieve.smp"));
+ CodeGen code = new CodeGen(src).parse().opto().typeCheck().instSelect(PORTS,"x86_64_v2", "SystemV").GCM().localSched();
assertEquals("return [int];", code.print());
//assertEquals("int[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20));
}
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java
index 7f151b15..1a08d6ee 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java
@@ -2,6 +2,9 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.print.ASMPrinter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -36,7 +39,9 @@ public class Chapter20Test {
}
@Test public void testAlloc0() {
- testAllCPUs("return new u8[arg];", 1, "return [u8];");
+ testCPU("return new u8[arg];","x86_64_v2", "SystemV",3,"return [u8];");
+ testCPU("return new u8[arg];","riscv" , "SystemV",7,"return [u8];");
+ testCPU("return new u8[arg];","arm" , "SystemV",7,"return [u8];");
}
@Test public void testBasic1() {
@@ -67,21 +72,9 @@ return sqrt(arg) + sqrt(arg+2);
}
@Test
- public void testNewtonFloat() {
- String src =
-"""
-// Newtons approximation to the square root
-val sqrt = { flt x ->
- flt guess = x;
- while( 1 ) {
- flt next = (x/guess + guess)/2;
- if( next == guess ) return guess;
- guess = next;
- }
-};
-flt farg = arg;
-return sqrt(farg) + sqrt(farg+2.0);
-""";
+ public void testNewtonFloat() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp"))
+ + "flt farg = arg; return sqrt(farg) + sqrt(farg+2.0);";
testCPU(src,"x86_64_v2", "SystemV",25,null);
testCPU(src,"riscv" , "SystemV",17,null);
testCPU(src,"arm" , "SystemV",18,null);
@@ -90,74 +83,36 @@ return sqrt(farg) + sqrt(farg+2.0);
@Test
public void testAlloc2() {
String src = "int[] !xs = new int[3]; xs[arg]=1; return xs[arg&1];";
- testAllCPUs(src,1,"return .[];");
- }
-
- @Test
- public void testArray1() {
- String src =
-"""
-int[] !ary = new int[arg];
-// Fill [0,1,2,3,4,...]
-for( int i=0; i<ary#; i++ )
- ary[i] = i;
-// Fill [0,1,3,6,10,...]
-for( int i=0; i<ary#-1; i++ )
- ary[i+1] += ary[i];
-return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
-""";
testCPU(src,"x86_64_v2", "SystemV",3,"return .[];");
- testCPU(src,"riscv" , "SystemV",1,"return (add,.[],(mul,.[],1000));");
- testCPU(src,"arm" , "SystemV",1,"return (add,.[],(mul,.[],1000));");
+ testCPU(src,"riscv" , "SystemV",6,"return .[];");
+ testCPU(src,"arm" , "SystemV",6,"return .[];");
}
- @Test
- public void testString() {
- String src = """
-struct String {
- u8[] cs;
- int _hashCode;
-};
-
-val equals = { String self, String s ->
- if( self == s ) return true;
- if( self.cs# != s.cs# ) return false;
- for( int i=0; i< self.cs#; i++ )
- if( self.cs[i] != s.cs[i] )
- return false;
- return true;
-};
-val hashCode = { String self ->
- self._hashCode
- ? self._hashCode
- : (self._hashCode = _hashCodeString(self));
-};
-val _hashCodeString = { String self ->
- int hash=0;
- if( self.cs ) {
- for( int i=0; i< self.cs#; i++ )
- hash = hash*31 + self.cs[i];
+ @Test
+ public void testArray1() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/array1.smp"));
+ testCPU(src,"x86_64_v2", "SystemV",7,"return .[];");
+ testCPU(src,"riscv" , "SystemV",8,"return (add,.[],(mul,.[],1000));");
+ testCPU(src,"arm" , "SystemV",5,"return (add,.[],(mul,.[],1000));");
}
- if( !hash ) hash = 123456789;
- return hash;
-};
-String !s = new String { cs = new u8[17]; };
-s.cs[0] = 67; // C
-s.cs[1] = 108; // l
-hashCode(s);
-""";
+
+ @Test
+ public void testString() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
testCPU(src,"x86_64_v2", "SystemV",18,null);
- testCPU(src,"riscv" , "SystemV", 3,null);
- testCPU(src,"arm" , "SystemV", 3,null);
+ testCPU(src,"riscv" , "SystemV",17,null);
+ testCPU(src,"arm" , "SystemV",16,null);
}
@Test
public void testCast() {
String src = "struct Bar { int x; }; var b = arg ? new Bar; return b ? b.x++ + b.x++ : -1;";
- testAllCPUs(src,0,null);
+ testCPU(src,"x86_64_v2", "SystemV",2,null);
+ testCPU(src,"riscv" , "SystemV",2,null);
+ testCPU(src,"arm" , "SystemV",2,null);
}
@Test
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
index 06988ee8..9d780b57 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
@@ -44,39 +44,26 @@ public class Chapter21Test {
testCPU(src,"arm" , "SystemV",0,"return (ori,arg);");
}
- public static final String newton = """
-// Newtons approximation to the square root
-val sqrt = { flt x ->
- flt guess = x;
- while( 1 ) {
- flt next = (x/guess + guess)/2;
- if( next == guess ) return guess;
- guess = next;
+ @Test
+ public void testNewtonFloat() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp"))
+ + "flt farg = arg; return sqrt(farg) + sqrt(farg+2.0);";
+ testCPU(src,"x86_64_v2", "SystemV",25,null);
+ testCPU(src,"riscv" , "SystemV",17,null);
+ testCPU(src,"arm" , "SystemV",18,null);
}
-};
-flt farg = arg;
-return sqrt(farg) + sqrt(farg+2.0);
-""";
@Test
- public void testNewtonFloat() {
- testCPU(newton,"x86_64_v2", "SystemV",25,null);
- testCPU(newton,"riscv" , "SystemV",17,null);
- testCPU(newton,"arm" , "SystemV",18,null);
+ public void testString() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
+ testCPU(src,"x86_64_v2", "SystemV",18,null);
+ testCPU(src,"riscv" , "SystemV", 3,null);
+ testCPU(src,"arm" , "SystemV", 3,null);
}
@Test
- public void testArray1() {
- String src = """
-int[] !ary = new int[arg];
-// Fill [0,1,2,3,4,...]
-for( int i=0; i<ary#; i++ )
- ary[i] = i;
-// Fill [0,1,3,6,10,...]
-for( int i=0; i<ary#-1; i++ )
- ary[i+1] += ary[i];
-return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
-""";
+ public void testArray1() throws IOException {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/array1.smp"));
testCPU(src,"x86_64_v2", "SystemV",3,"return .[];");
testCPU(src,"riscv" , "SystemV",1,"return (add,.[],(mul,.[],1000));");
testCPU(src,"arm" , "SystemV",1,"return (add,.[],(mul,.[],1000));");
@@ -85,7 +72,9 @@ return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
@Test
public void testExport() throws IOException {
- CodeGen code = new CodeGen(newton);
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp"))
+ + "flt farg = arg; return sqrt(farg) + sqrt(farg+2.0);";
+ CodeGen code = new CodeGen(src);
code.parse().opto().typeCheck().instSelect(PORTS, "x86_64_v2", "SystemV").GCM().localSched().regAlloc().encode().exportELF("build/objs/newton.o");
}
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/array1.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/array1.smp
new file mode 100644
index 00000000..6b10f92c
--- /dev/null
+++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/array1.smp
@@ -0,0 +1,9 @@
+// -*- mode: java; -*-
+int[] !ary = new int[arg];
+// Fill [0,1,2,3,4,...]
+for( int i=0; i<ary#; i++ )
+ ary[i] = i;
+// Fill [0,1,3,6,10,...]
+for( int i=0; i<ary#-1; i++ )
+ ary[i+1] += ary[i];
+return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/fcn2.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/fcn2.smp
deleted file mode 100644
index 049132d9..00000000
--- a/chapter21/src/test/java/com/seaofnodes/simple/progs/fcn2.smp
+++ /dev/null
@@ -1,5 +0,0 @@
-
-val fcn = (arg&1)
- ? { int x -> x*x; }
- : { int x -> x+x; };
-fcn(arg);
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/fcn3.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/fcn3.smp
deleted file mode 100644
index a64564f0..00000000
--- a/chapter21/src/test/java/com/seaofnodes/simple/progs/fcn3.smp
+++ /dev/null
@@ -1,42 +0,0 @@
-
-//val fact = { int n -> n <= 1 ? 1 : fact(n-1)*n; }
-struct LL { LL? next; int payload; };
-
-
-struct String {
-
- val _hashCodeString = { ->
- int hash=0;
- for( int i=0; i< cs#; i++ )
- hash = hash*31 + cs[i];
- if( !hash ) hash = 123456789;
- return hash;
- };
-
- val equals = { String s ->
- if( self == s ) return true;
- if( cs# != s.cs# ) return false;
- for( int i=0; i< #; i++ )
- if( [i] != s.cs[i] )
- return false;
- return true;
- };
-
- int _hashCode = hash();
- u8[] cs;
-};
-
-
-
-val hashCode= { String self ->
- int hash = self._hashCode
- ? self._hashCode
- : self._hashCode = _hashCodeString(self);
- return hash;
-};
-
-
-String s = new String { cs = new u8[17]; };
-s.cs[0] = 67; // C
-s.cs[1] = 108; // l
-hashCode(s);
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp
new file mode 100644
index 00000000..5cb798a9
--- /dev/null
+++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/newtonFloat.smp
@@ -0,0 +1,10 @@
+// -*- mode: java; -*-
+// Newtons approximation to the square root
+val sqrt = { flt x ->
+ flt guess = x;
+ while( 1 ) {
+ flt next = (x/guess + guess)/2;
+ if( next == guess ) return guess;
+ guess = next;
+ }
+};
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp
index 0788e6c5..0b6f56ed 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp
+++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/sieve.smp
@@ -1,32 +1,29 @@
-
-int[] !ary = new int[arg], !primes = new int[arg];
+// -*- mode: java; -*-
+// The main Sieve array
+bool[] !ary = new bool[arg];
+// The primes less than arg
+int[] !primes = new int[arg>>1];
+// Number of primes so far, searching at index p
int nprimes = 0, p=2;
// Find primes while p^2 < arg
while( p*p < arg ) {
// skip marked non-primes
- while( ary[p]==1 ) p = p + 1;
+ while( ary[p] ) p++;
// p is now a prime
- primes[nprimes] = p; nprimes = nprimes + 1;
+ primes[nprimes++] = p;
// Mark out the rest non-primes
- int i = p + p;
- while( i < ary# ) {
- ary[i] = 1;
- i = i + p;
- }
- p = p + 1;
+ for( int i = p + p; i < ary#; i+= p )
+ ary[i] = true;
+ p++;
}
+
// Now just collect the remaining primes, no more marking
-while( p < arg ) {
- if( ary[p] == 0 ) {
- primes[nprimes] = p; nprimes = nprimes + 1;
- }
- p = p + 1;
-}
+for( ; p < arg; p++ )
+ if( !ary[p] )
+ primes[nprimes++] = p;
+
// Copy/shrink the result array
int[] !rez = new int[nprimes];
-int j = 0;
-while( j < nprimes ) {
+for( int j=0; j < nprimes; j++ )
rez[j] = primes[j];
- j = j + 1;
-}
return rez;
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/progs/stringHash.smp b/chapter21/src/test/java/com/seaofnodes/simple/progs/stringHash.smp
new file mode 100644
index 00000000..455e0014
--- /dev/null
+++ b/chapter21/src/test/java/com/seaofnodes/simple/progs/stringHash.smp
@@ -0,0 +1,36 @@
+// -*- mode: java; -*-
+struct String {
+ u8[] cs;
+ int _hashCode;
+};
+
+// Compare two Strings
+val equals = { String self, String s ->
+ if( self == s ) return true;
+ if( self.cs# != s.cs# ) return false;
+ for( int i=0; i< self.cs#; i++ )
+ if( self.cs[i] != s.cs[i] )
+ return false;
+ return true;
+};
+
+// Return the String hashCode (cached, and never 0)
+val hashCode = { String self ->
+ self._hashCode
+ ? self._hashCode
+ : (self._hashCode = _hashCodeString(self));
+};
+
+val _hashCodeString = { String self ->
+ int hash=0;
+ if( self.cs )
+ for( int i=0; i< self.cs#; i++ )
+ hash = hash*31 + self.cs[i];
+ if( !hash ) hash = 123456789;
+ return hash;
+};
+
+String !s = new String { cs = new u8[17]; };
+s.cs[0] = 67; // C
+s.cs[1] = 108; // l
+hashCode(s)+hashCode(s);