Skip to content

Commit

Permalink
chapter 16 extra credit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsandler committed Jun 5, 2024
1 parent 78626fc commit 556172b
Show file tree
Hide file tree
Showing 26 changed files with 536 additions and 3 deletions.
2 changes: 1 addition & 1 deletion expected_results.json

Large diffs are not rendered by default.

78 changes: 76 additions & 2 deletions test_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,87 @@
"compound"
],
"chapter_15/valid/extra_credit/compound_assign_and_increment.c": [
"compound", "increment"
"compound",
"increment"
],
"chapter_15/valid/extra_credit/compound_bitwise_subscript.c": [
"compound", "bitwise"
"compound",
"bitwise"
],
"chapter_15/valid/extra_credit/bitwise_subscript.c": [
"bitwise"
],
"chapter_16/invalid_parse/extra_credit/string_literal_label.c": [
"goto"
],
"chapter_16/invalid_parse/extra_credit/string_literal_goto.c": [
"goto"
],
"chapter_16/invalid_parse/extra_credit/character_const_goto.c": [
"goto"
],
"chapter_16/invalid_parse/extra_credit/character_const_label.c": [
"goto"
],
"chapter_16/invalid_types/extra_credit/bit_shift_string.c": [
"bitwise"
],
"chapter_16/invalid_types/extra_credit/bitwise_operation_on_string.c": [
"bitwise"
],
"chapter_16/invalid_types/extra_credit/case_statement_string.c": [
"switch"
],
"chapter_16/invalid_types/extra_credit/switch_on_string.c": [
"switch"
],
"chapter_16/invalid_types/extra_credit/compound_assign_from_string.c": [
"compound"
],
"chapter_16/invalid_types/extra_credit/compound_assign_to_string.c": [
"compound"
],
"chapter_16/invalid_types/extra_credit/postfix_incr_string.c": [
"increment"
],
"chapter_16/invalid_labels/extra_credit/duplicate_case_char_const.c": [
"switch"
],
"chapter_16/invalid_types/extra_credit/prefix_incr_string.c": [
"increment"
],
"chapter_16/valid/extra_credit/incr_decr_chars.c": [
"increment"
],
"chapter_16/valid/extra_credit/incr_decr_unsigned_chars.c": [
"increment"
],
"chapter_16/valid/extra_credit/compound_assign_chars.c": [
"compound"
],
"chapter_16/valid/extra_credit/bitshift_chars.c": [
"bitwise"
],
"chapter_16/valid/extra_credit/bitwise_ops_chars.c": [
"bitwise"
],
"chapter_16/valid/extra_credit/bitwise_ops_character_constants.c": [
"bitwise"
],
"chapter_16/valid/extra_credit/compound_bitwise_ops_chars.c": [
"bitwise", "compound"
],
"chapter_16/valid/extra_credit/promote_switch_cond.c": [
"switch"
],
"chapter_16/valid/extra_credit/promote_switch_cond_2.c": [
"switch"
],
"chapter_16/valid/extra_credit/switch_on_char_const.c": [
"switch"
],
"chapter_16/valid/extra_credit/char_consts_as_cases.c": [
"switch"
]
},
"requires_mathlib": [
Expand Down
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;
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
}
38 changes: 38 additions & 0 deletions tests/chapter_16/valid/extra_credit/bitshift_chars.c
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

}
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;
}
29 changes: 29 additions & 0 deletions tests/chapter_16/valid/extra_credit/bitwise_ops_chars.c
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 tests/chapter_16/valid/extra_credit/char_consts_as_cases.c
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 tests/chapter_16/valid/extra_credit/compound_assign_chars.c
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;
}
Loading

0 comments on commit 556172b

Please sign in to comment.