https://github.com/akkartik/mu/blob/master/034address.cc
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 :(before "End Globals")
43 long long Next_alloc_id = 0;
44 :(before "End Reset")
45 Next_alloc_id = 0;
46
47
48
49
50
51
52
53
54
55 :(scenario new)
56
57
58 def main [
59 10:&:num <- new num:type
60 12:&:num <- new num:type
61 20:bool <- equal 10:&:num, 12:&:num
62 ]
63 +mem: storing 1000 in location 11
64 +mem: storing 0 in location 20
65
66 :(scenario new_array)
67
68 def main [
69 10:&:@:num <- new num:type, 5
70 12:&:num <- new num:type
71 20:num/alloc2, 21:num/alloc1 <- deaddress 10:&:@:num, 12:&:num
72 30:num <- subtract 21:num/alloc2, 20:num/alloc1
73 ]
74 +run: {10: ("address" "array" "number")} <- new {num: "type"}, {5: "literal"}
75 +mem: array length is 5
76
77 +mem: storing 1000 in location 11
78
79 +mem: storing 7 in location 30
80
81 :(scenario dilated_reagent_with_new)
82 def main [
83 10:&:&:num <- new {(& num): type}
84 ]
85 +new: size of '(& num)' is 2
86
87
88 :(before "End Mu Types Initialization")
89 put(Type_ordinal, "type", 0);
90 :(code)
91 bool is_mu_type_literal(const reagent& r) {
92 return is_literal(r) && r.type && r.type->name == "type";
93 }
94
95 :(before "End Primitive Recipe Declarations")
96 NEW,
97 :(before "End Primitive Recipe Numbers")
98 put(Recipe_ordinal, "new", NEW);
99 :(before "End Primitive Recipe Checks")
100 case NEW: {
101 const recipe& caller = get(Recipe, r);
102 if (inst.ingredients.empty() || SIZE(inst.ingredients) > 2) {
103 raise << maybe(caller.name) << "'new' requires one or two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
104 break;
105 }
106
107 const reagent& type = inst.ingredients.at(0);
108 if (!is_mu_type_literal(type)) {
109 raise << maybe(caller.name) << "first ingredient of 'new' should be a type, but got '" << type.original_string << "'\n" << end();
110 break;
111 }
112 if (SIZE(inst.ingredients) > 1 && !is_mu_number(inst.ingredients.at(1))) {
113 raise << maybe(caller.name) << "second ingredient of 'new' should be a number (array length), but got '" << type.original_string << "'\n" << end();
114 break;
115 }
116 if (inst.products.empty()) {
117 raise << maybe(caller.name) << "result of 'new' should never be ignored\n" << end();
118 break;
119 }
120 if (!product_of_new_is_valid(inst)) {
121 raise << maybe(caller.name) << "product of 'new' has incorrect type: '" << to_original_string(inst) << "'\n" << end();
122 break;
123 }
124 break;
125 }
126 :(code)
127 bool product_of_new_is_valid(const instruction& inst) {
128 reagent product = inst.products.at(0);
129
130 if (!product.type || product.type->atom || product.type->left->value != Address_type_ordinal)
131 return false;
132 drop_from_type(product, "address");
133 if (SIZE(inst.ingredients) > 1) {
134
135 if (!product.type || product.type->atom || product.type->left->value != Array_type_ordinal)
136 return false;
137 drop_from_type(product, "array");
138 }
139 reagent expected_product(new_type_tree(inst.ingredients.at(0).name));
140 return types_strictly_match(product, expected_product);
141 }
142
143 void drop_from_type(reagent& r, string expected_type) {
144 assert(!r.type->atom);
145 if (r.type->left->name != expected_type) {
146 raise << "can't drop2 " << expected_type << " from '" << to_string(r) << "'\n" << end();
147 return;
148 }
149
150 type_tree* tmp = r.type;
151 r.type = tmp->right;
152 tmp->right = NULL;
153 delete tmp;
154
155 assert(!r.type->atom);
156 if (r.type->right) return;
157 tmp = r.type;
158 r.type = tmp->left;
159 tmp->left = NULL;
160 delete tmp;
161 }
162
163 :(scenario new_returns_incorrect_type)
164 % Hide_errors = true;
165 def main [
166 1:bool <- new num:type
167 ]
168 +error: main: product of 'new' has incorrect type: '1:bool <- new num:type'
169
170 :(scenario new_discerns_singleton_list_from_atom_container)
171 % Hide_errors = true;
172 def main [
173 1:&:num <- new {(num): type}
174 ]
175 +error: main: product of 'new' has incorrect type: '1:&:num <- new {(num): type}'
176
177 :(scenario new_with_type_abbreviation)
178 def main [
179 1:&:num <- new num:type
180 ]
181 $error: 0
182
183 :(scenario new_with_type_abbreviation_inside_compound)
184 def main [
185 {1: (address address number), raw: ()} <- new {(& num): type}
186 ]
187 $error: 0
188
189 :(scenario equal_result_of_new_with_null)
190 def main [
191 1:&:num <- new num:type
192 10:bool <- equal 1:&:num, null
193 ]
194 +mem: storing 0 in location 10
195
196
197
198
199
200
201
202 :(before "End Primitive Recipe Checks")
203 case ALLOCATE: {
204 raise << "never call 'allocate' directly'; always use 'new'\n" << end();
205 break;
206 }
207 :(before "End Primitive Recipe Implementations")
208 case NEW: {
209 raise << "no implementation for 'new'; why wasn't it translated to 'allocate'? Please save a copy of your program and send it to Kartik.\n" << end();
210 break;
211 }
212
213 :(after "Transform.push_back(check_instruction)")
214 Transform.push_back(transform_new_to_allocate);
215
216 :(code)
217 void transform_new_to_allocate(const recipe_ordinal r) {
218 trace(9991, "transform") << *str = p_contact_create_display_string(contact, "__prof_default");
assert_string_equal("bob", str);
p_contact_free(contact);
free(str);
}
void contact_presence_offline(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
const char *presence = p_contact_presence(contact);
assert_string_equal("offline", presence);
p_contact_free(contact);
}
void contact_presence_uses_highest_priority(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
Resource *resource10 = resource_new("resource10", RESOURCE_ONLINE, NULL, 10);
Resource *resource20 = resource_new("resource20", RESOURCE_CHAT, NULL, 20);
Resource *resource30 = resource_new("resource30", RESOURCE_AWAY, NULL, 30);
Resource *resource1 = resource_new("resource1", RESOURCE_XA, NULL, 1);
Resource *resource2 = resource_new("resource2", RESOURCE_DND, NULL, 2);
p_contact_set_presence(contact, resource10);
p_contact_set_presence(contact, resource20);
p_contact_set_presence(contact, resource30);
p_contact_set_presence(contact, resource1);
p_contact_set_presence(contact, resource2);
const char *presence = p_contact_presence(contact);
assert_string_equal("away", presence);
p_contact_free(contact);
}
void contact_presence_chat_when_same_prioroty(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
Resource *resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
Resource *resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
Resource *resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
p_contact_set_presence(contact, resource_online);
p_contact_set_presence(contact, resource_chat);
p_contact_set_presence(contact, resource_away);
p_contact_set_presence(contact, resource_xa);
p_contact_set_presence(contact, resource_dnd);
const char *presence = p_contact_presence(contact);
assert_string_equal("chat", presence);
p_contact_free(contact);
}
void contact_presence_online_when_same_prioroty(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
Resource *resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
Resource *resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
p_contact_set_presence(contact, resource_online);
p_contact_set_presence(contact, resource_away);
p_contact_set_presence(contact, resource_xa);
p_contact_set_presence(contact, resource_dnd);
const char *presence = p_contact_presence(contact);
assert_string_equal("online", presence);
p_contact_free(contact);
}
void contact_presence_away_when_same_prioroty(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
Resource *resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
p_contact_set_presence(contact, resource_away);
p_contact_set_presence(contact, resource_xa);
p_contact_set_presence(contact, resource_dnd);
const char *presence = p_contact_presence(contact);
assert_string_equal("away", presence);
p_contact_free(contact);
}
void contact_presence_xa_when_same_prioroty(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
p_contact_set_presence(contact, resource_xa);
p_contact_set_presence(contact, resource_dnd);
const char *presence = p_contact_presence(contact);
assert_string_equal("xa", presence);
p_contact_free(contact);
}
void contact_presence_dnd(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
p_contact_set_presence(contact, resource_dnd);
const char *presence = p_contact_presence(contact);
assert_string_equal("dnd", presence);
p_contact_free(contact);
}
void contact_subscribed_when_to(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "to",
"is offline", FALSE);
gboolean result = p_contact_subscribed(contact);
assert_true(result);
p_contact_free(contact);
}
void contact_subscribed_when_both(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
"is offline", FALSE);
gboolean result = p_contact_subscribed(contact);
assert_true(result);
p_contact_free(contact);
}
void contact_not_subscribed_when_from(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, "from",
"is offline", FALSE);
gboolean result = p_contact_subscribed(contact);
assert_false(result);
p_contact_free(contact);
}
void contact_not_subscribed_when_no_subscription_value(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
"is offline", FALSE);
gboolean result = p_contact_subscribed(contact);
assert_false(result);
p_contact_free(contact);
}
void contact_not_available(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
"is offline", FALSE);
gboolean result = p_contact_is_available(contact);
assert_false(result);
p_contact_free(contact);
}
void contact_not_available_when_highest_priority_away(void **state)
{
PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
"is offline", FALSE);
Resource *resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
Resource *resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);