-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
277 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
tests/chapter_10/invalid_types/extra_credit/static_var_case.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
tests/chapter_10/valid/extra_credit/bitwise_ops_file_scope_vars.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/chapter_10/valid/extra_credit/compound_assignment_static_var.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
tests/chapter_10/valid/extra_credit/increment_global_vars.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/chapter_10/valid/extra_credit/label_file_scope_var_same_name.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/chapter_10/valid/extra_credit/label_static_var_same_name.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/chapter_10/valid/extra_credit/libraries/same_label_same_fun.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/chapter_10/valid/extra_credit/libraries/same_label_same_fun_client.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/chapter_10/valid/extra_credit/switch_skip_extern_decl.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
16 changes: 16 additions & 0 deletions
16
tests/chapter_10/valid/extra_credit/switch_skip_static_initializer.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |