summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/Chapter15Test.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2025-04-24 14:38:36 -0700
committerCliff Click <cliffc@acm.org>2025-04-24 14:38:36 -0700
commitcadc530688041b5455e6251055754df6f4182aec (patch)
tree1f83d6d9285adb0c10c9147da3e64700e8ea2820 /src/test/java/com/seaofnodes/simple/Chapter15Test.java
parentf6c0b12e6cc1af4879042c0426fc96532cdfddc3 (diff)
downloadSimple-linear-chapter21.tar.gz
Simple-linear-chapter21.zip
Chapter 21: Instruction Encoding and ELFlinear-chapter21
Co-authored-by: Co-authored-by: Cliff Click <cliffc@acm.org> Co-authored-by: Co-authored-by: godalming123 <68993177+godalming123@users.noreply.github.com>
Diffstat (limited to 'src/test/java/com/seaofnodes/simple/Chapter15Test.java')
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter15Test.java49
1 files changed, 10 insertions, 39 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index 7dd0710a..8b138b46 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/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.*;
@@ -127,7 +130,7 @@ else {
return rez;
""");
code.parse().opto();
- assertEquals("return Phi(Region,1.2,Phi(Region,2.3,.y));", code.print());
+ assertEquals("return Phi(Region,1.2,.y,2.3);", code.print());
assertEquals("3.14", Eval2.eval(code, 0));
assertEquals("1.2", Eval2.eval(code, 1));
}
@@ -152,7 +155,7 @@ if( iss[arg] )
rez = iss[arg][2].y;
return rez;""");
code.parse().opto().typeCheck();
- assertEquals("return Phi(Region,Phi(Region,.y,1.2),1.2);", code.print());
+ assertEquals("return Phi(Region,1.2,1.2,.y);", code.print());
assertEquals("3.14", Eval2.eval(code, 0));
try { Eval2.eval(code, 10); fail(); } // Fails AIOOBE in Eval2, no range checks
catch( Exception e ) { assertEquals("Index 10 out of bounds for length 2",e.getMessage()); }
@@ -210,44 +213,12 @@ 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 {
+ String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/sieve.smp"));
+ CodeGen code = new CodeGen(src+"return sieve(arg);");
code.parse().opto();
- assertEquals("return [int];", code.print());
- assertEquals("int[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20));
+ assertEquals("return [u32];", code.print());
+ assertEquals("u32[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20));
}
@Test