Skip to content

Commit

Permalink
addressing feedback on chapter 11/12 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsandler committed Feb 7, 2024
1 parent 14fb2e8 commit 125a59d
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion expected_results.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/chapter_11/invalid_parse/long_constant_as_var.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
int main(void) {
/* You can't use a long constant where a variable is required */
/* You can't use a long constant where an identifier is required */
int 10l;
return 0;
}
8 changes: 4 additions & 4 deletions tests/chapter_11/valid/extra_credit/bitwise_long_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ int main(void) {
if (i >= 0) {
/* use bitwise "and" to zero out upper bits */
if ((l & lower_32_bits_set) != i)
return 0;
return 1;
} else {
/* use bitwise "or" to set upper bits */
if ((l | upper_32_bits_set) != i)
return 0;
return 2;
}

/* every bit is set in -1, so l & -1 == l */
if ((l & -1) != l)
return 0;
return 3;
}
return 1;
return 0; // success
}
6 changes: 3 additions & 3 deletions tests/chapter_11/valid/extra_credit/switch_long.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ int switch_on_long(long l) {

int main(void) {
if (switch_on_long(8589934592) != 2)
return 0;
return 1;
if (switch_on_long(100) != 1)
return 0;
return 1;
return 2;
return 0; // success
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* Test that function arguments, including arguments put on the stack,
* are converted to the corresponding parameter type */

int foo(long a, int b, long c, int d, long e, int f, long g, int h) {
int foo(long a, int b, int c, int d, long e, int f, long g, int h) {
if (a != -1l)
return 1;

if (b != 2)
return 2;

if (c != -4294967296l)
if (c != 0)
return 3;

if (d != -5)
Expand All @@ -31,12 +31,14 @@ int foo(long a, int b, long c, int d, long e, int f, long g, int h) {

int main(void) {
int a = -1;
long int b = 4294967298; // 2^32 + 2, becomes 2 when converted to an int
long c = -4294967296;
long d = 21474836475; // 2^34 + 2^32 - 5, becomes -5 when converted to an int
long int b = 4294967298; // 2^32 + 2, becomes 2 when converted to an int
long c = -4294967296; // -2^32, becoems 0 when converted to int
long d =
21474836475; // 2^34 + 2^32 - 5, becomes -5 when converted to an int
int e = -101;
long f = -123;
int g = -10;
long h = -9223372036854774574; // -2^63 + 1234, becomes 1234 when converted to an int
long h = -9223372036854774574; // -2^63 + 1234, becomes 1234 when converted
// to an int
return foo(a, b, c, d, e, f, g, h);
}
2 changes: 1 addition & 1 deletion tests/chapter_11/valid/libraries/long_args_client.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* This is identical to the test case in tests/chapter12/valid/long_expressions/long_args.c,
/* This is essentially the same as the test case in tests/chapter11/valid/long_expressions/long_args.c,
* but split across multiple files.
*/

Expand Down
6 changes: 3 additions & 3 deletions tests/chapter_11/valid/libraries/return_long_client.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* This is identical to the test case in tests/chapter12/valid/long_expressions/return_long.c,
/* This is identical to the test case in tests/chapter11/valid/long_expressions/return_long.c,
* but split across multiple files.
*/

Expand All @@ -7,8 +7,8 @@ long add(int a, int b);
int main(void) {
long a = add(2147483645, 2147483645);
/* Test returning a long from a function call */
if (a == 4294967290l) {
if (a != 4294967290l) {
return 1;
}
return 0;
return 0; // success
}
6 changes: 4 additions & 2 deletions tests/chapter_12/valid/explicit_casts/round_trip_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ unsigned long a = 8589934580ul; // 2^33 - 12

int main(void) {

/* casting to unsigned int and back reduces is equivalent to subtracting
/* because a is too large to fit in an unsigned int,
* casting it to unsigned int and back is equivalent to subtracting
* 2^32, resulting in 4294967284
*/
unsigned long b = (unsigned long) (unsigned int) a;

if (b != 4294967284ul)
return 1;

/* Casting to a signed int and back results in 2^64 - 12,
/* Casting a to signed int results in -12, and
* casting it back to unsigned long results in 2^64 - 12,
* or 18446744073709551604
*/
b = (unsigned long) (signed int) a;
Expand Down
11 changes: 5 additions & 6 deletions tests/chapter_12/valid/extra_credit/bitwise_unsigned_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(void) {
* 2. calculate the bitwise and of this zero-extended value and ul. the result is 0
*/
if ((ui & ul) != 0)
return 0;
return 1;

/* this expression will:
* 1. zero-extend ui. the result will have all 32 lower bits set to 1
Expand All @@ -20,7 +20,7 @@ int main(void) {
* the result is 2^63 + 2^32 - 1
*/
if ((ui | ul) != 9223372041149743103ul)
return 0;
return 2;

signed int i = -1;
/* this expression will:
Expand All @@ -29,7 +29,7 @@ int main(void) {
* the result is equal to ul.
*/
if ((i & ul) != ul)
return 0;
return 3;


/* this expression will:
Expand All @@ -38,8 +38,7 @@ int main(void) {
* the result will have every bit set
*/
if ((i | ul) != i)
return 0;

return 1;
return 4;

return 0; // success
}
7 changes: 5 additions & 2 deletions tests/chapter_12/valid/extra_credit/bitwise_unsigned_shift.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
int main(void) {
unsigned int ui = -1u; // 2^32 - 1, or 4294967295

/* shifting right by 2 is like subtracting 3.
/* shifting left by 2 is like subtracting 3.
* note that we don't cast ui to a long first
*/
return ((ui << 2l) == 4294967292);
if ((ui << 2l) != 4294967292) {
return 1;
}
return 0; // success
}
2 changes: 1 addition & 1 deletion tests/chapter_12/valid/implicit_casts/common_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int ternary_int_uint(int flag, int i, unsigned int ui) {
* (we don't consider the type of cond when we
* determine the common type).
* We therefore convert i to an unsigned int, 2^32 - 1,
* which we then convert o a signed long.
* which we then convert to a signed long.
* Therefore, result will be positive. If we didn't
* convert i to an unsigned int, result would be negative.
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/chapter_12/valid/implicit_casts/static_initializers.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ int main(void)
return 4;
if (ul != 4294967294ul)
return 5;
if (i2 != -2147483498)
return 6;
if (ul2 != 9223372036854775798ul)
return 6;
if (i2 != -2147483498)
return 7;
if (ui2 != 2147483798u)
return 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int division_large_dividend(void) {

/* upper bit of ui_a is set, so this tests
* that we zero-extended dividend into EDX
* instead of sign_extending it
* instead of sign extending it
*/

return (ui_a / ui_b == 2);
Expand Down
2 changes: 1 addition & 1 deletion tests/chapter_16/valid/chars/chained_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ unsigned int ui = 4294967200u; // 2^32 - 96

int main(void) {
/* 1. convert ui to an unsigned char with value 256 - 96
* 2. convert it back to an unsig ed int, which preserves its value.
* 2. convert it back to an unsigned int, which preserves its value.
*/
ui = (unsigned int)(unsigned char)ui;
if (ui != 160) {
Expand Down

0 comments on commit 125a59d

Please sign in to comment.