Skip to content

Commit

Permalink
chapter 10 extra credit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsandler committed May 9, 2024
1 parent 116cfa3 commit 1147bd4
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 5 deletions.
2 changes: 1 addition & 1 deletion expected_results.json

Large diffs are not rendered by default.

47 changes: 43 additions & 4 deletions test_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@
"switch"
],
"chapter_8/invalid_semantics/extra_credit/duplicate_case_in_labeled_switch.c": [
"switch", "goto"
"switch",
"goto"
],
"chapter_8/invalid_semantics/extra_credit/duplicate_default.c": [
"switch"
Expand All @@ -363,7 +364,8 @@
"switch"
],
"chapter_8/invalid_semantics/extra_credit/undefined_label_in_case.c": [
"switch", "goto"
"switch",
"goto"
],
"chapter_8/invalid_semantics/extra_credit/duplicate_case_in_nested_statement.c": [
"switch"
Expand All @@ -375,7 +377,8 @@
"switch"
],
"chapter_8/invalid_semantics/extra_credit/duplicate_label_in_default.c": [
"switch", "goto"
"switch",
"goto"
],
"chapter_8/valid/extra_credit/compound_assignment_for_loop.c": [
"compound"
Expand Down Expand Up @@ -531,15 +534,51 @@
"chapter_9/valid/extra_credit/label_naming_scheme.c": [
"goto"
],
"chapter_10/invalid_parse/extra_credit/goto_file_scope_label.c": [
"chapter_10/invalid_parse/extra_credit/file_scope_label.c": [
"goto"
],
"chapter_10/invalid_parse/extra_credit/static_label.c": [
"goto"
],
"chapter_10/invalid_parse/extra_credit/extern_label.c": [
"goto"
],
"chapter_10/invalid_labels/extra_credit/goto_global_var.c": [
"goto"
],
"chapter_10/invalid_types/extra_credit/static_var_case.c": [
"switch"
],
"chapter_10/valid/extra_credit/goto_skip_static_initializer.c": [
"goto"
],
"chapter_10/valid/extra_credit/switch_skip_static_initializer.c": [
"switch"
],
"chapter_10/valid/extra_credit/bitwise_ops_file_scope_vars.c": [
"bitwise"
],
"chapter_10/valid/extra_credit/increment_global_vars.c": [
"increment"
],
"chapter_10/valid/extra_credit/label_file_scope_var_same_name.c": [
"goto"
],
"chapter_10/valid/extra_credit/label_static_var_same_name.c": [
"goto"
],
"chapter_10/valid/extra_credit/libraries/same_label_same_fun.c": [
"goto"
],
"chapter_10/valid/extra_credit/switch_on_extern.c": [
"switch"
],
"chapter_10/valid/extra_credit/switch_skip_extern_decl.c": [
"switch"
],
"chapter_10/valid/extra_credit/compound_assignment_static_var.c": [
"compound"
],
"chapter_11/invalid_labels/extra_credit/switch_duplicate_cases.c": [
"switch"
],
Expand Down
6 changes: 6 additions & 0 deletions tests/chapter_10/invalid_parse/extra_credit/extern_label.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// The extern specifier cannot be applied to labels

int main(void) {
extern a:
return 1;
}
6 changes: 6 additions & 0 deletions tests/chapter_10/invalid_parse/extra_credit/static_label.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// The static specifier cannot be applied to labels

int main(void) {
static a:
return 1;
}
10 changes: 10 additions & 0 deletions tests/chapter_10/invalid_types/extra_credit/static_var_case.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Can't use a static variable as a case in a switch statement

int main(void) {
static int i = 0;

switch(0) {
case i: return 0;
}
return 0;
}
17 changes: 17 additions & 0 deletions tests/chapter_10/valid/extra_credit/bitwise_ops_file_scope_vars.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Test that we can perform bitwise operations on file-scope variables

int x = 1;
int y = 0;

int main(void) {
y = -1;
x = (x << 1) | 1; // x = 3
if (x != 3) {
return 1;
}
y = ((y & -5) ^ 12) >> 2; // y = -3
if (y != -3) {
return 2;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
int f(void) {
static int i = 0;
static int j = 0;
static int k = 1;
static int l = 48;
i += 1;
j -= i;
k *= j;
l /= 2;

// expected values after 3 invocations:
// i = 3
// j = -6
// k = -18
// l = 6
if (i != 3) {
return 1;
}
if (j != -6) {
return 2;
}
if (k != -18) {
return 3;
}
if (l != 6) {
return 4;
}
return 0;
}

int main(void) {
f();
f();
return f();
}
43 changes: 43 additions & 0 deletions tests/chapter_10/valid/extra_credit/increment_global_vars.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Test ++ and -- operations on global variables;
int i = 0;
int j = 0;

int incr_i(void){
// expect i = 1
if (i == 1) {
i++;
++i;
return 0;
}
}

int decr_j(void) {
// expect j = -1
if (j == -1) {
j--;
}
return 0;
}

int main(void) {
// should take second branch; result of i++ is value before incrementing (i.e. 0)
// but we evaluate the branch after the side effect of incrementing the value
i++ ? 0 : incr_i();

// after fun call, expect i = 3
if (i != 3) {
// fail
return 1;
}

// should take first branch; result of --j is value after decrementing
--j? decr_j(): 0;

// after fun call, expect j = -2
if (j != -2) {
// fail
return 2;
}

return 0; // success
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// file scope variables and labels are in different namespaces,
// so they can share the same name

int x; // file scope var - initialized to 0

int main(void) {
int x = 10; // declare a local var
goto x; // refers to label, not either variable
return x;
{
// bring global var back into scope, shadowing local one
// NOTE: this also tests that we correctly resolve variable names
// even when we jump over their declarations
extern int x;
x: // label
return x; // global var - should return 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// a static variable and label within the same function can share a name
// (make sure we don't e.g. use the naming scheme "main.x" in both cases)
int main(void) {
static int x = 5;
goto x;
x = 0;
x:
return x; // return 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
static int f(void) {
goto x;
return 0;
x:
return 2;
}

int f_caller(void) {
return f();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// make sure we can handle two labels with the same ID, in two functions with
// the same name, in different translation units

int f(void) {
goto x;
return 0;
x:
return 1;
}

int f_caller(void); // declared in same_label_same_fun.c

int main(void) {
if (f() != 1) {
return 1; // fail
}
if (f_caller() !=
2) { // call "f" with internal linkage in other translation unit
return 2; // fail
}
return 0; // success
}
21 changes: 21 additions & 0 deletions tests/chapter_10/valid/extra_credit/switch_on_extern.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
int update_x(void);

// test that we can use an external variable in a switch statement
int main(void) {
update_x(); // set x to 4
extern int x; // bring x into scope
switch(x) {
case 0: return 1; // fail
case 1: return 2; // fail
case 4: return 0; // success!
default: return 4; // fail

}
}

int x;

int update_x(void) {
x = 4;
return 0;
}
20 changes: 20 additions & 0 deletions tests/chapter_10/valid/extra_credit/switch_skip_extern_decl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// an external variable is in scope inside a switch statement
// even if we jump over the point where it's declared

int main(void) {
int a = 10;
switch(a) {
case 1: return 1; // fail
extern int x; // bring x into scope
case 2: return 2; // fail
case 10:
if (x * 2 == 30) {
return 0; // success
}
default: return 5; // fail
}
return 6; // also fail; shouldn't have made it to this point
}


int x = 15;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
int a = 3;
int main(void) {
switch (a) {
case 1:;
/* Since x is static, it's initialized at program startup,
* so its value will be 10 even though we jump over this declaration
*/
static int x = 10;
// we DON'T execute this, since it's a statement rather than a
// static initializer
x = 0;
case 3:
return x; // expected return value: 10
}
return 0; // fail
}

0 comments on commit 1147bd4

Please sign in to comment.