Skip to content

Commit e52db58

Browse files
committed
More specials tests
1 parent 982e087 commit e52db58

File tree

2 files changed

+67
-45
lines changed

2 files changed

+67
-45
lines changed

convex-core/src/test/java/convex/core/lang/CoreTest.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.HashMap;
3232

3333
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.TestInstance;
35+
import org.junit.jupiter.api.TestInstance.Lifecycle;
3436

3537
import convex.core.Coin;
3638
import convex.core.Constants;
@@ -103,6 +105,7 @@
103105
* Needs completely deterministic, fully specified behaviour if we want
104106
* consistent results so we need to do a lot of negative testing here.
105107
*/
108+
@TestInstance(Lifecycle.PER_CLASS)
106109
public class CoreTest extends ACVMTest {
107110

108111
protected CoreTest() throws IOException {
@@ -5125,49 +5128,7 @@ public void testSqrt() {
51255128
assertCastError(step("(sqrt false)"));
51265129
}
51275130

5128-
@Test
5129-
public void testSpecialKey() {
5130-
assertEquals(InitTest.HERO_KEYPAIR.getAccountKey(), eval("*key*"));
5131-
}
5132-
5133-
@Test
5134-
public void testSpecialPeer() {
5135-
assertNull(eval("*peer*"));
5136-
}
51375131

5138-
@Test
5139-
public void testSpecialJuice() {
5140-
// TODO: semantics of returning juice before lookup complete is OK?
5141-
// seems sensible, represents "juice left at this position".
5142-
assertCVMEquals(0, eval(Special.forSymbol(Symbols.STAR_JUICE)));
5143-
5144-
// juice gets consumed before returning a value
5145-
assertCVMEquals(Juice.DO + Juice.CONSTANT, eval(comp("(do 1 *juice*)")));
5146-
}
5147-
5148-
@Test
5149-
public void testSpecialJuiceLimit() {
5150-
Special<CVMLong> spec=Special.forSymbol(Symbols.STAR_JUICE_LIMIT);
5151-
5152-
// Juice limit at start of transaction
5153-
assertCVMEquals(Constants.MAX_TRANSACTION_JUICE, eval(spec));
5154-
5155-
// Consuming a small amount of juice shouldn't change limit
5156-
Context ctx=step("1");
5157-
assertCVMEquals(Constants.MAX_TRANSACTION_JUICE, eval(ctx,"*juice-limit*"));
5158-
}
5159-
5160-
5161-
@Test
5162-
public void testSpecialJuicePrice() {
5163-
Special<?> jp=Special.forSymbol(Symbols.STAR_JUICE_PRICE);
5164-
assertNotNull(jp);
5165-
assertCVMEquals(Constants.INITIAL_JUICE_PRICE, eval(jp));
5166-
5167-
assertCVMEquals(Constants.INITIAL_JUICE_PRICE, eval("*juice-price*"));
5168-
5169-
assertSame(context().getState().getJuicePrice(),eval("*juice-price*"));
5170-
}
51715132

51725133

51735134

convex-core/src/test/java/convex/core/lang/SpecialTest.java

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import static convex.test.Assertions.assertCVMEquals;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
56
import static org.junit.jupiter.api.Assertions.assertNull;
67
import static org.junit.jupiter.api.Assertions.assertSame;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89

910
import java.io.IOException;
1011

1112
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.TestInstance;
14+
import org.junit.jupiter.api.TestInstance.Lifecycle;
1215

1316
import convex.core.Constants;
1417
import convex.core.data.Address;
@@ -20,6 +23,7 @@
2023
import convex.core.lang.ops.Lookup;
2124
import convex.core.lang.ops.Special;
2225

26+
@TestInstance(Lifecycle.PER_CLASS)
2327
public class SpecialTest extends ACVMTest {
2428

2529

@@ -46,22 +50,75 @@ public void testSpecialOrigin() {
4650
// Hero should be *origin* in initial context
4751
assertEquals(InitTest.HERO, eval("*origin*"));
4852

53+
// Origin should be preserved across query-as?
54+
assertEquals(InitTest.HERO, eval("(query-as #8 '*origin*)"));
55+
4956
// *origin* MUST return original address within actor call
5057
Context ctx=step("(def act (deploy `(do (defn origin ^{:callable true} [] *origin*))))");
5158
assertEquals(InitTest.HERO, eval(ctx,"(call act (origin))"));
5259

5360
// *origin* MUST be original address in library call
5461
assertEquals(InitTest.HERO, eval(ctx,"(act/origin)"));
5562
}
63+
64+
@Test
65+
public void testSpecialKey() {
66+
assertEquals(InitTest.HERO_KEYPAIR.getAccountKey(), eval("*key*"));
67+
}
68+
69+
@Test
70+
public void testSpecialJuice() {
71+
// TODO: semantics of returning juice before lookup complete is OK?
72+
// seems sensible, represents "juice left at this position".
73+
assertCVMEquals(0, eval(Special.forSymbol(Symbols.STAR_JUICE)));
74+
75+
// juice gets consumed before returning a value
76+
assertCVMEquals(Juice.DO + Juice.CONSTANT, eval(comp("(do 1 *juice*)")));
77+
}
78+
79+
@Test
80+
public void testSpecialJuiceLimit() {
81+
Special<CVMLong> spec=Special.forSymbol(Symbols.STAR_JUICE_LIMIT);
82+
83+
// Juice limit at start of transaction
84+
assertCVMEquals(Constants.MAX_TRANSACTION_JUICE, eval(spec));
85+
86+
// Consuming a small amount of juice shouldn't change limit
87+
Context ctx=step("1");
88+
assertCVMEquals(Constants.MAX_TRANSACTION_JUICE, eval(ctx,"*juice-limit*"));
89+
}
90+
91+
@Test
92+
public void testSpecialJuicePrice() {
93+
Special<?> jp=Special.forSymbol(Symbols.STAR_JUICE_PRICE);
94+
assertNotNull(jp);
95+
assertCVMEquals(Constants.INITIAL_JUICE_PRICE, eval(jp));
96+
97+
assertCVMEquals(Constants.INITIAL_JUICE_PRICE, eval("*juice-price*"));
98+
99+
assertSame(context().getState().getJuicePrice(),eval("*juice-price*"));
100+
}
101+
102+
@Test
103+
public void testSpecialPeer() {
104+
assertNull(eval("*peer*"));
105+
}
106+
107+
@Test
108+
public void testSpecialSigned() {
109+
assertNull(eval("*signer*"));
110+
}
56111

57112
@Test
58113
public void testSpecialAllowance() {
59114
// Should have initial allowance at start
60115
assertEquals(Constants.INITIAL_ACCOUNT_ALLOWANCE, evalL("*memory*"));
61116

62117
// Buy some memory
63-
assertEquals(Constants.INITIAL_ACCOUNT_ALLOWANCE, evalL("*memory*"));
118+
assertEquals(Constants.INITIAL_ACCOUNT_ALLOWANCE+1, evalL("(do (set-memory (inc *memory*)) *memory*)"));
64119

120+
// Sell all memory
121+
assertEquals(0, evalL("(do (set-memory 0) *memory*)"));
65122
}
66123

67124
@Test
@@ -74,6 +131,9 @@ public void testSpecialMemoryPrice() {
74131
// Buy some memory, should increase price
75132
c=exec(c,"(set-memory (+ *memory* 10))");
76133
assertTrue(price<evalD(c,"*memory-price*"));
134+
135+
// Check memory went down
136+
assertTrue(evalL(c,"*balance*")<HERO_BALANCE);
77137
}
78138

79139

@@ -166,8 +226,9 @@ public void testSpecialScope() {
166226
@Test
167227
public void testSpecialEdgeCases() {
168228

169-
// TODO: consider this
170-
//assertEquals(Init.HERO,eval(Init.CORE_ADDRESS+"/*balance*"));
229+
// query-as interactions with *balance*
230+
assertEquals(0,evalL("(query-as #8 '*balance*)"));
231+
assertEquals(HERO_BALANCE,evalL("(query-as #8 '(query-as *caller* '*balance*))"));
171232

172233
// Lookup in core environment of special returns the corresponding Special Op
173234
assertSame(Special.get("*juice*"),eval("(lookup *juice*)"));

0 commit comments

Comments
 (0)