-
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
26 changed files
with
536 additions
and
3 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
13 changes: 13 additions & 0 deletions
13
tests/chapter_16/invalid_labels/extra_credit/duplicate_case_char_const.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,13 @@ | ||
// Reject duplicate cases where one uses an integer constant and one uses | ||
// a character constant | ||
int main(void) { | ||
static int i = 120; | ||
switch (i) { | ||
case 'x': // ASCII value 120 | ||
return 1; | ||
case 120: // duplicate | ||
return 2; | ||
default: | ||
return 3; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/chapter_16/invalid_parse/extra_credit/character_const_goto.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,6 @@ | ||
// You can't use a character constant as a label in a goto statement | ||
int main(void) { | ||
goto 'x'; | ||
'x'; | ||
return 0; | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/chapter_16/invalid_parse/extra_credit/character_const_label.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,4 @@ | ||
// You can't use a character constant as a label | ||
int main(void) { | ||
'x': return 0; | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/chapter_16/invalid_parse/extra_credit/string_literal_goto.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,5 @@ | ||
// You can't use a string literal as a label in a goto statement | ||
int main(void) { | ||
goto "foo"; | ||
return 0; | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/chapter_16/invalid_parse/extra_credit/string_literal_label.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,4 @@ | ||
// You can't use a string literal as a label | ||
int main(void) { | ||
"foo": return 0; | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/chapter_16/invalid_types/extra_credit/bit_shift_string.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,5 @@ | ||
// can't apply << or >> to strings | ||
int main(void) { | ||
"foo" << 3; | ||
return 0; | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/chapter_16/invalid_types/extra_credit/bitwise_operation_on_string.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,5 @@ | ||
// Can't apply bitwise operations to strings | ||
int main(void) { | ||
"My string" & 100; | ||
return 0; | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/chapter_16/invalid_types/extra_credit/case_statement_string.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 @@ | ||
// Can't use strings as labels for case statements | ||
int main(void) { | ||
switch (0) { | ||
case "foo": | ||
return 1; | ||
default: | ||
return 0; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/chapter_16/invalid_types/extra_credit/compound_assign_from_string.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,6 @@ | ||
// Can't use a string on the right side of a compound assignment expression | ||
int main(void) { | ||
char * s = "some string "; | ||
s += "another str"; | ||
return 0; | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/chapter_16/invalid_types/extra_credit/compound_assign_to_string.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,5 @@ | ||
// Can't compound assign to string literal | ||
int main(void) { | ||
"My string" += 1; | ||
return 0; | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/chapter_16/invalid_types/extra_credit/postfix_incr_string.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,5 @@ | ||
// can't apply postfix ++/-- to string literals | ||
int main(void) { | ||
"foo"++; | ||
return 0; | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/chapter_16/invalid_types/extra_credit/prefix_incr_string.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,5 @@ | ||
// Can't apply prefix ++/-- to string literal | ||
int main(void) { | ||
++"foo"; | ||
return 0; | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/chapter_16/invalid_types/extra_credit/switch_on_string.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,7 @@ | ||
// can't use string literal as controlling expression in switch statement | ||
int main(void) { | ||
switch ("foo") { | ||
default: | ||
return 0; | ||
} | ||
} |
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,38 @@ | ||
// Test << and >> operators with chars (or mix of chars and other types) | ||
// Make sure left operand is promoted from char to int (NOTE: right operand should | ||
// be too but we can't verify that; it doesn't change the operand's value or | ||
// the result value.) | ||
|
||
int main(void) { | ||
unsigned char uc = 255; | ||
|
||
// uc is promoted to int | ||
//if ((uc << 20) != 267386880) { | ||
// return 1; // fail | ||
// } | ||
|
||
// uc is promoted to int, then shifted | ||
if ((uc >> 3) != 31) { | ||
return 2; // fail | ||
} | ||
|
||
signed char sc = -127; | ||
char c = 5; | ||
// sc is promoted to int, then shifted | ||
if ((sc >> c) != -4) { | ||
return 3; // fail | ||
} | ||
|
||
// make sure c << 3ul is promoted to int, not unsigned long | ||
if (((-(c << 3ul)) >> 3) != -5) { | ||
return 4; // fail | ||
} | ||
|
||
// make sure uc << 5u is promoted to int, not unsigned int | ||
if ((-(uc << 5u) >> 5u) != -255l) { | ||
return 5; // fail | ||
} | ||
|
||
return 0; // fail | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
tests/chapter_16/valid/extra_credit/bitwise_ops_character_constants.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 @@ | ||
#ifdef SUPPRESS_WARNINGS | ||
#ifdef __clang__ | ||
#pragma clang diagnostic ignored "-Wconstant-conversion" | ||
#else | ||
#pragma GCC diagnostic ignored "-Woverflow" | ||
#endif | ||
#endif | ||
|
||
// Test that we can use character constants in bitwise operations | ||
int main(void) { | ||
int x = 10; | ||
if ((x ^ 'A') != 75) { | ||
return 1; // fail | ||
} | ||
|
||
static char c = 132; // converted to -124 | ||
if (('!' | c) != -91) { | ||
return 2; // fail | ||
} | ||
|
||
static unsigned long ul = 9259400834947493926ul; | ||
if ((ul & '~') != 38) { | ||
return 3; // fail | ||
} | ||
|
||
if ((ul << ' ') != 4611738958194278400ul) { | ||
return 4; // fail | ||
} | ||
|
||
if (('{' >> 3) != 15) { | ||
return 5; // fail | ||
} | ||
|
||
return 0; | ||
} |
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,29 @@ | ||
// make sure we perform integer promotions when performing bitwise operations on | ||
// chars | ||
|
||
int main(void) { | ||
unsigned char uc = 135; | ||
char c = -116; | ||
// Make sure we FIRST promote, THEN perform bitwise op | ||
// if we performed the bitwise op first and then sign-extended, | ||
// we'd get a different result | ||
if ((uc & c) != 132) { | ||
return 1; // fail | ||
} | ||
|
||
// Make sure we FIRST promote, THEN perform bitwise op | ||
// if we performed the bitwise op first and then zero-extended, | ||
// we'd get a different result | ||
if ((uc | c) != -113) { | ||
return 2; // fail | ||
} | ||
|
||
// make sure we do usual conversion to common type | ||
// (result of c ^ 1001u is unsigned int, so we zero-extend it | ||
// for next operation) | ||
if (((c ^ 1001u) | 360l) != 4294966637) { | ||
return 3; // fail | ||
} | ||
|
||
return 0; | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/chapter_16/valid/extra_credit/char_consts_as_cases.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 @@ | ||
// Test that we can use character constants as cases in switch statements | ||
int main(void) { | ||
static int i = 65; | ||
switch (i) { | ||
case 100l: | ||
return 1; // fail | ||
case 'A': | ||
return 0; // success | ||
case 'B': | ||
return 2; // fail | ||
case 2000u: | ||
return 3; // fail | ||
default: | ||
return -1; // fail | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/chapter_16/valid/extra_credit/compound_assign_chars.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 @@ | ||
// Test compound assignment with characters; make sure we perform integer promotions | ||
|
||
int main(void) { | ||
|
||
char c = 100; | ||
char c2 = 100; | ||
c += c2; // well-defined b/c of integer promotions | ||
if (c != -56) { | ||
return 1; // fail | ||
} | ||
|
||
unsigned char uc = 200; | ||
c2 = -100; | ||
uc /= c2; // convert uc and c2 to int, then convert back | ||
if (uc != 254) { | ||
return 2; // fail | ||
} | ||
|
||
uc -= 250.0; // convert uc to double, do operation, convert back | ||
if (uc != 4) { | ||
return 3; // fail | ||
} | ||
|
||
signed char sc = -70; | ||
sc *= c; | ||
if (sc != 80) { | ||
return 4; // fail | ||
} | ||
|
||
if ((sc %= c) != 24) { | ||
return 5; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.