Files
@ 9774ef9fe888
Branch filter:
Location: CSY/reowolf/src/protocol/tests/parser_inference.rs
9774ef9fe888
4.5 KiB
application/rls-services+xml
small cleanup pass, added (failing) monomorph test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | /// parser_inference.rs
///
/// Simple tests for the type inferences
use super::*;
#[test]
fn test_integer_inference() {
Tester::new_single_source_expect_ok(
"by arguments",
"
int call(byte b, short s, int i, long l) {
auto b2 = b;
auto s2 = s;
auto i2 = i;
auto l2 = l;
return i2;
}
"
).for_function("call", |f| { f
.for_variable("b2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("s2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("short");
})
.for_variable("i2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("int");
})
.for_variable("l2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("long");
});
});
Tester::new_single_source_expect_ok(
"by assignment",
"
int call() {
byte b1 = 0; short s1 = 0; int i1 = 0; long l1 = 0;
auto b2 = b1;
auto s2 = s1;
auto i2 = i1;
auto l2 = l1;
return 0;
}"
).for_function("call", |f| { f
.for_variable("b2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("s2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("short");
})
.for_variable("i2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("int");
})
.for_variable("l2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("long");
});
});
}
#[test]
fn test_struct_inference() {
Tester::new_single_source_expect_ok(
"by function calls",
"
struct Pair<T1, T2>{ T1 first, T2 second }
Pair<T1, T2> construct<T1, T2>(T1 first, T2 second) {
return Pair{ first: first, second: second };
}
int fix_t1<T2>(Pair<byte, T2> arg) { return 0; }
int fix_t2<T1>(Pair<T1, int> arg) { return 0; }
int test() {
auto first = 0;
auto second = 1;
auto pair = construct(first, second);
fix_t1(pair);
fix_t2(pair);
return 0;
}
"
).for_function("test", |f| { f
.for_variable("first", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("second", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("int");
})
.for_variable("pair", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("Pair<byte,int>");
});
});
Tester::new_single_source_expect_ok(
"by field access",
"
struct Pair<T1, T2>{ T1 first, T2 second }
Pair<T1, T2> construct<T1, T2>(T1 first, T2 second) {
return Pair{ first: first, second: second };
}
int test() {
auto first = 0;
auto second = 1;
auto pair = construct(first, second);
byte assign_first = 0;
long assign_second = 1;
pair.first = assign_first;
pair.second = assign_second;
return 0;
}
"
).for_function("test", |f| { f
.for_variable("first", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("second", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("long");
})
.for_variable("pair", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("Pair<byte,long>");
});
});
Tester::new_single_source_expect_ok(
"by nested field access",
"
struct Node<T1, T2>{ T1 l, T2 r }
Node<T1, T2> construct<T1, T2>(T1 l, T2 r) { return Node{ l: l, r: r }; }
int fix_poly<T>(Node<T, T> a) { return 0; }
int test() {
byte assigned = 0;
auto thing = construct(assigned, construct(0, 1));
fix_poly(thing.r);
thing.r.r = assigned;
return 0;
}
",
).for_function("test", |f| { f
.for_variable("thing", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("Node<byte,Node<byte,byte>>");
});
});
}
|