Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zlargon/b33c35138e54ebb90eb0 to your computer and use it in GitHub Desktop.
Save zlargon/b33c35138e54ebb90eb0 to your computer and use it in GitHub Desktop.
Test true and false with type 'bool' and 'int'
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define test_bool(var) _test_bool(var, #var)
void _test_bool(bool var, const char * var_name) {
printf("%s => %d\n", var_name, var);
printf("true ? %s\n", true == var ? "YES" : "NO");
printf("false ? %s\n\n", false == var ? "YES" : "NO");
}
#define test_int(var) _test_int(var, #var)
void _test_int(int var, const char * var_name) {
printf("%s => %d\n", var_name, var);
printf("true ? %s\n", true == var ? "YES" : "NO");
printf("false ? %s\n\n", false == var ? "YES" : "NO");
}
int main() {
puts("test_bool:\n");
test_bool(-1);
test_bool(1);
test_bool(0);
test_bool(10);
puts("-----------\n");
puts("test_int:\n");
test_int(-1);
test_int(1);
test_int(0);
test_int(10);
return EXIT_SUCCESS;
}
/*
* Result:
*
* test_bool:
*
* -1 => 1
* true ? YES
* false ? NO
*
* 1 => 1
* true ? YES
* false ? NO
*
* 0 => 0
* true ? NO
* false ? YES
*
* 10 => 1
* true ? YES
* false ? NO
*
* -----------
*
* test_int:
*
* -1 => -1
* true ? NO
* false ? NO
*
* 1 => 1
* true ? YES
* false ? NO
*
* 0 => 0
* true ? NO
* false ? YES
*
* 10 => 10
* true ? NO
* false ? NO
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment