section .text global is_square ; C-style declaration: ``_Bool is_square();`` extern sqrt ; C-style declaration: ``double sqrt(double);`` is_square: cvtsi2sd xmm0, rdi ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first call sqrt ; now, XMM0 = sqrt(XMM0) cvtsd2si rax, xmm0 ; RAX = (int) XMM0 mul rax ; RAX *= RAX xor rax, rdi ; if this XOR evaluates to 0, then RAX == RDI setz al ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true" ret ; return to the caller
def is_square(n):return (n ** 0.5).is_integer()- section .text
- global is_square ; C-style declaration: ``_Bool is_square();``
- extern sqrt ; C-style declaration: ``double sqrt(double);``
- is_square:
- cvtsi2sd xmm0, rdi ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first
- call sqrt ; now, XMM0 = sqrt(XMM0)
- cvtsd2si rax, xmm0 ; RAX = (int) XMM0
- mul rax ; RAX *= RAX
- xor rax, rdi ; if this XOR evaluates to 0, then RAX == RDI
- setz al ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true"
- ret ; return to the caller
#include <criterion/criterion.h> _Bool is_square(unsigned); static unsigned i, smallest_32_squares[32] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961 }; Test(the_perfect_square_checker, return_truthy_for_perfect_squares) { for (i = 0; i < 32; i++) cr_assert(is_square(smallest_32_squares[i])); } Test(the_perfect_square_checker, return_falsy_for_imperfect_squares) { for (i = 0; i < 32; i++) cr_assert_not(is_square(smallest_32_squares[i] + 2)); }
import codewars_test as testfrom solution import is_square- #include <criterion/criterion.h>
- _Bool is_square(unsigned);
@test.describe("Example")def test_group():@test.it("test case")def test_case():perfect_squares_samples = (4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900,961)non_perfect_squares_samples = [n + 1 for n in perfect_squares_samples]for s in perfect_squares_samples:test.assert_equals(is_square(s), True)for s in non_perfect_squares_samples:test.assert_equals(is_square(s), False)- static unsigned i, smallest_32_squares[32] = {
- 0, 1, 4, 9, 16, 25, 36, 49,
- 64, 81, 100, 121, 144, 169, 196, 225,
- 256, 289, 324, 361, 400, 441, 484, 529,
- 576, 625, 676, 729, 784, 841, 900, 961
- };
- Test(the_perfect_square_checker, return_truthy_for_perfect_squares) {
- for (i = 0; i < 32; i++) cr_assert(is_square(smallest_32_squares[i]));
- }
- Test(the_perfect_square_checker, return_falsy_for_imperfect_squares) {
- for (i = 0; i < 32; i++) cr_assert_not(is_square(smallest_32_squares[i] + 2));
- }
#define This "mulsd" #define is "%xmm2," #define a "%xmm1\n" #define strangely This #define fascinating "%xmm1," #define way "%xmm0\n" #define to "leave\n" #define multiply "ret\n" double kuboid() { __asm(This is a strangely fascinating way to multiply); return 0; }
def kube(x, y, z):#your code herereturn x*y*z- #define This "mulsd"
- #define is "%xmm2,"
- #define a "%xmm1\n"
- #define strangely This
- #define fascinating "%xmm1,"
- #define way "%xmm0\n"
- #define to "leave\n"
- #define multiply "ret\n"
- double kuboid() {
- __asm(This is a strangely fascinating way to multiply);
- return 0;
- }
#include <criterion/criterion.h> double kuboid(double, double, double); #define assert_kuboid(x, y, z, v) \ do { \ cr_assert_float_eq(kuboid((x), (y), (z)), (v), 1e-12); \ } while (0) Test(the_volume_function, should_pass_these_basic_tests) { assert_kuboid(7., 9., 4., 252.); assert_kuboid(5., 12., 7., 420.); assert_kuboid(3., 7., 49., 1029.); }
import codewars_test as test# TODO Write testsfrom solution import kube- #include <criterion/criterion.h>
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(kube(7, 9, 4), 252)test.assert_equals(kube(5, 12, 7), 420)test.assert_equals(kube(3, 7, 49), 1029)- double kuboid(double, double, double);
- #define assert_kuboid(x, y, z, v) \
- do { \
- cr_assert_float_eq(kuboid((x), (y), (z)), (v), 1e-12); \
- } while (0)
- Test(the_volume_function, should_pass_these_basic_tests) {
- assert_kuboid(7., 9., 4., 252.);
- assert_kuboid(5., 12., 7., 420.);
- assert_kuboid(3., 7., 49., 1029.);
- }
#include <criterion/criterion.h> int foo; Test(variable_foo, should_be_zero) { cr_assert_eq(foo, 0); }
- #include <criterion/criterion.h>
- int foo;
Test(function_foo, should_return_zero) {cr_assert_eq(foo, 0);}Test(function_bar, should_return_zero) {cr_assert_eq(foo, 0);}Test(function_baz, should_return_zero) {- Test(variable_foo, should_be_zero) {
- cr_assert_eq(foo, 0);
- }
C: 20 bytes (13 if you reduce time_out
to one letter)
#include <criterion/criterion.h> int time_out(); Test(the_nothing_function, should_NOT_pass_the_test) { cr_assert_not(time_out()); }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- #include <criterion/criterion.h>
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(time_out(), None)- int time_out();
- Test(the_nothing_function, should_NOT_pass_the_test) {
- cr_assert_not(time_out());
- }
global invertbits invertbits db 137, ~7, 52, ~0, 'Ã'
- global invertbits
section .text; <--- unsigned invertbits(const int num) --->; rdi=numinvertbits:xor rax, rax ; EAX <- the resultnot rdimov rax, rdiret; ---------> endof invertbits <---------- invertbits db 137, ~7, 52, ~0, 'Ã'
#include <inttypes.h> #include <criterion/criterion.h> uint8_t invertbits(uint8_t); #define _assert_inv(x) cr_assert_eq(invertbits((x)), 255 ^ (x)) Test(the_invert_function, should_pass_all_the_tests) { _assert_inv(0); for (uint8_t i = 1; i; i++) _assert_inv(i); }
- #include <inttypes.h>
- #include <criterion/criterion.h>
// replace with the actual method being testedunsigned char invertbits(unsigned char value);- uint8_t invertbits(uint8_t);
Test(the_multiply_function, should_pass_all_the_tests_provided) {cr_assert_eq(invertbits(3), 252);cr_assert_eq(invertbits(255), 0);cr_assert_eq(invertbits(7), 248);cr_assert_eq(invertbits(128), 127);- #define _assert_inv(x) cr_assert_eq(invertbits((x)), 255 ^ (x))
- Test(the_invert_function, should_pass_all_the_tests) {
- _assert_inv(0);
- for (uint8_t i = 1; i; i++)
- _assert_inv(i);
- }
section .text global riddle extern strlen riddle: inc rdi jmp strlen ; this works, but it's on very thin ice
def riddle(word): return word.index('?')- section .text
- global riddle
- extern strlen
- riddle:
- inc rdi
- jmp strlen
- ; this works, but it's on very thin ice
#include <criterion/criterion.h> int riddle(const char *); Test(basic_cases, pass_these_tests) { cr_assert_eq(riddle("Zero?"), 4); cr_assert_eq(riddle("One?"), 3); cr_assert_eq(riddle("Two?"), 3); cr_assert_eq(riddle("Three?"), 5); cr_assert_eq(riddle("Four?"), 4); cr_assert_eq(riddle("Five?"), 4); cr_assert_eq(riddle("Six?"), 3); cr_assert_eq(riddle("Seven?"), 5); cr_assert_eq(riddle("Eight?"), 5); cr_assert_eq(riddle("Nine?"), 4); }
test.assert_equals(riddle("Two?"), 3)test.assert_equals(riddle("Ten?"), 3)test.assert_equals(riddle("Three?"), 5)- #include <criterion/criterion.h>
- int riddle(const char *);
- Test(basic_cases, pass_these_tests) {
- cr_assert_eq(riddle("Zero?"), 4);
- cr_assert_eq(riddle("One?"), 3);
- cr_assert_eq(riddle("Two?"), 3);
- cr_assert_eq(riddle("Three?"), 5);
- cr_assert_eq(riddle("Four?"), 4);
- cr_assert_eq(riddle("Five?"), 4);
- cr_assert_eq(riddle("Six?"), 3);
- cr_assert_eq(riddle("Seven?"), 5);
- cr_assert_eq(riddle("Eight?"), 5);
- cr_assert_eq(riddle("Nine?"), 4);
- }
"Are you using Assembly?"
"Yesn't"
__asm( " .data \n" " one_half: .long 1056964608 \n\n" " .text \n" " .global area_of_triangle \n\n" " area_of_triangle: \n" " mulss %xmm1, %xmm0 \n" " mulss one_half(%rip), %xmm0 \n" " ret " );
float AreaOfTriangle(float w, float h) {float A = (w * h) / 2;return A;}- __asm(
- " .data \n"
- " one_half: .long 1056964608 \n\n"
- " .text \n"
- " .global area_of_triangle \n\n"
- " area_of_triangle: \n"
- " mulss %xmm1, %xmm0 \n"
- " mulss one_half(%rip), %xmm0 \n"
- " ret "
- );
#include <criterion/criterion.h> float area_of_triangle(float base, float height); Test(the_area_calculator, calculate_the_area) { cr_assert_eq(area_of_triangle(4., 3.), 6.); cr_assert_eq(area_of_triangle(6., 10.), 30.); cr_assert_eq(area_of_triangle(10., 2.), 10.); cr_assert_eq(area_of_triangle(16., 8.), 64.); cr_assert_eq(area_of_triangle(25., 45.), 562.5); }
Describe(any_group_name_you_want){It(should_do_something){Assert::That(AreaOfTriangle(4, 3), Equals(6));Assert::That(AreaOfTriangle(6, 10), Equals(30));Assert::That(AreaOfTriangle(10, 2), Equals(10));}};- #include <criterion/criterion.h>
- float area_of_triangle(float base, float height);
- Test(the_area_calculator, calculate_the_area) {
- cr_assert_eq(area_of_triangle(4., 3.), 6.);
- cr_assert_eq(area_of_triangle(6., 10.), 30.);
- cr_assert_eq(area_of_triangle(10., 2.), 10.);
- cr_assert_eq(area_of_triangle(16., 8.), 64.);
- cr_assert_eq(area_of_triangle(25., 45.), 562.5);
- }
Given an integer n
return whether or not the integer is even. Your code may not contain any numbers, or spaces.
e.g.
is_even(2) => True
is_even(5) => False
is_even(123456) => True
is_even(654321) => False
is_even=lambda*z:not(z[ord('`')-ord('`')]%(ord('a')//ord('!')))
is_even=lambda n:not(n%(ord('ࠀ')>>ord('\n'))) # I can't get out the final space!- is_even=lambda*z:not(z[ord('`')-ord('`')]%(ord('a')//ord('!')))
import codewars_test as test @test.describe('Fixed Tests') def example_tests(): @test.it('Code Test Case') def check_code(): txt = open('/workspace/solution.txt').read() if any(n in txt for n in "0123456789 "): test.fail("Your code may not contain any digits (0-9) or spaces!") test.pass_() @test.it('Example Test Case') def simple_tests(): for i in range(1,21): test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
- import codewars_test as test
import re, random- @test.describe('Fixed Tests')
- def example_tests():
@test.it('Submission Tests')- @test.it('Code Test Case')
- def check_code():
# regex subs for line commentstxt = re.sub('(\s#.*)','', (open('/workspace/solution.txt').read()))txt = re.sub('^\s+', '', txt, flags=re.MULTILINE)txt = txt.split(':',maxsplit=1)[1]print("Evaluated code:\n\t{}".format('\n\t'.join(repr(line)for line in txt.split('\n'))))if any(n in txt for n in "0123456789"):test.fail("Your code may contain any digits (0-9)!")if txt.count(' ') > 1:test.fail("Your code may only contain one space (' ') character!")- txt = open('/workspace/solution.txt').read()
- if any(n in txt for n in "0123456789 "):
- test.fail("Your code may not contain any digits (0-9) or spaces!")
- test.pass_()
@test.it('Test Cases')- @test.it('Example Test Case')
- def simple_tests():
- for i in range(1,21):
- test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
@test.describe('Random Tests')def random_tests():@test.it("Random Small Numbers")def small_ints():for _ in range(20):i = random.randint(0,101)test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')@test.it("Random Big Numbers")def big_ints():for _ in range(20):i = random.randint(1e9,10e9)test.assert_equals(is_even(i), i%2==0, f'{i} should equal {i%2==0}')
This thing should have no right to run, let alone compile...
is_even(){__asm("mov%rdi,%rax\nshr%rax\nshl%rax\ncmp%rax,%rdi\nsete%al\nleave\nret");}
bool isEven(int n){return n % 2 == 0;}- is_even(){__asm("mov%rdi,%rax\nshr%rax\nshl%rax\ncmp%rax,%rdi\nsete%al\nleave\nret");}
#include <criterion/criterion.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> _Bool is_even(int); Test(the_even_checker, return_true_for_even_numbers) { int evens[] = {2, 456, 987654}; cr_assert(is_even(evens[0])); cr_assert(is_even(evens[1])); cr_assert(is_even(evens[2])); } Test(the_even_checker, return_false_for_odd_numbers) { int odds[] = {3, 123, 987654321}; cr_assert_not(is_even(0[odds])); cr_assert_not(is_even(1[odds])); cr_assert_not(is_even(2[odds])); } Test(the_even_checker, should_not_contain_spaces_or_numbers) { FILE *fp = fopen("/workspace/solution.c", "r"); if (!fp) exit(8); int c; while ((c = getc(fp)) != EOF) { if (isspace(c) | isdigit(c)) { fclose(fp); cr_assert_fail("Spaces and numbers are not allowed here"); } } fclose(fp); cr_assert(1); }
void testEqual(bool answer, bool solution){Assert::That(answer, Equals(solution));- #include <criterion/criterion.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- _Bool is_even(int);
- Test(the_even_checker, return_true_for_even_numbers) {
- int evens[] = {2, 456, 987654};
- cr_assert(is_even(evens[0]));
- cr_assert(is_even(evens[1]));
- cr_assert(is_even(evens[2]));
- }
Describe(isEven_Tests){It(test_even_numbers){testEqual(isEven(2), true);testEqual(isEven(456), true);testEqual(isEven(987654), true);}It(test_odd_numbers){testEqual(isEven(3), false);testEqual(isEven(123), false);testEqual(isEven(987654321), false);- Test(the_even_checker, return_false_for_odd_numbers) {
- int odds[] = {3, 123, 987654321};
- cr_assert_not(is_even(0[odds]));
- cr_assert_not(is_even(1[odds]));
- cr_assert_not(is_even(2[odds]));
- }
- Test(the_even_checker, should_not_contain_spaces_or_numbers) {
- FILE *fp = fopen("/workspace/solution.c", "r");
- if (!fp) exit(8);
- int c;
- while ((c = getc(fp)) != EOF) {
- if (isspace(c) | isdigit(c)) {
- fclose(fp);
- cr_assert_fail("Spaces and numbers are not allowed here");
- }
- }
};- fclose(fp);
- cr_assert(1);
- }
going by the assumption that 0 ** 0 = 1
section .text global power power: xor rax, rax or rdi, rdi jz edge_case_zero inc rax cmp rdi, rax je edge_case_one or rsi, rsi jnz multiply ret edge_case_zero: ret edge_case_one: ret multiply: mul rdi dec rsi jnz multiply ret
function power (numberToRaiseToThePower, powerToRaiseTheNumberTo) {if (powerToRaiseTheNumberTo === [].length || numberToRaiseToThePower === [undefined].length) return [{...{}}].length;let thisVariableWillBeTheFinalProduct = ["_"].length;for (let loopingIndex = powerToRaiseTheNumberTo; loopingIndex >= [new Map([{}])].length; loopingIndex -= [()=>{}].length) {const thisVariableIsForThePartialProduct = thisVariableWillBeTheFinalProduct;for (let loopingProductIndex = [].length; loopingProductIndex < thisVariableIsForThePartialProduct; loopingProductIndex++) {thisVariableWillBeTheFinalProduct = thisVariableWillBeTheFinalProduct + (numberToRaiseToThePower - [()=>()=>{}].length);}}return thisVariableWillBeTheFinalProduct;}- section .text
- global power
- power:
- xor rax, rax
- or rdi, rdi
- jz edge_case_zero
- inc rax
- cmp rdi, rax
- je edge_case_one
- or rsi, rsi
- jnz multiply
- ret
- edge_case_zero: ret
- edge_case_one: ret
- multiply:
- mul rdi
- dec rsi
- jnz multiply
- ret
#include <criterion/criterion.h> #include <limits.h> #include <time.h> #include <stddef.h> unsigned long power(unsigned long, unsigned long); int i; #define test_pow(a, b, c) cr_assert_eq(power((a), (b)), (c)) Test(sample_cases, should_work_with_positive_bases_and_exponents) { test_pow(1, 4, 1); test_pow(2, 3, 8); test_pow(2, 2, 4); test_pow(104, 7, 131593177923584); } Test(sample_zero_exponent_cases, should_return_one_instantly) { srand(time(NULL)); for (int i = 0; i < 50; i++) { test_pow(rand() << 32, 0, 1); } } Test(edge_cases_with_base_zero, should_not_time_out) { srand(time(NULL)); for (i = 0; i < 50; i++) test_pow(0, rand() << 32, 0); }
const chai = require("chai");const assert = chai.assert;chai.config.truncateThreshold=0;- #include <criterion/criterion.h>
- #include <limits.h>
- #include <time.h>
- #include <stddef.h>
describe("power", function() {it("test1", function() {assert.strictEqual(power( 1,4 ), 1);});it("test2", function() {assert.strictEqual(power( 2,3 ), 8);});it("test3", function() {assert.strictEqual(power( 2,2 ), 4);});it("test4", function() {- unsigned long power(unsigned long, unsigned long);
- int i;
assert.strictEqual(power( 2,0 ), 1);});});- #define test_pow(a, b, c) cr_assert_eq(power((a), (b)), (c))
- Test(sample_cases, should_work_with_positive_bases_and_exponents) {
- test_pow(1, 4, 1);
- test_pow(2, 3, 8);
- test_pow(2, 2, 4);
- test_pow(104, 7, 131593177923584);
- }
- Test(sample_zero_exponent_cases, should_return_one_instantly) {
- srand(time(NULL));
- for (int i = 0; i < 50; i++) {
- test_pow(rand() << 32, 0, 1);
- }
- }
- Test(edge_cases_with_base_zero, should_not_time_out) {
- srand(time(NULL));
- for (i = 0; i < 50; i++)
- test_pow(0, rand() << 32, 0);
- }
section .text global assert_equal ; AL assert_equal (EDI x, ESI y) assert_equal: xor eax, eax cmp edi, esi sete al ret
def assert_equal(param1, param2):assert param1 == param2- section .text
- global assert_equal ; AL assert_equal (EDI x, ESI y)
- assert_equal:
- xor eax, eax
- cmp edi, esi
- sete al
- ret
#include <criterion/criterion.h> #include <string.h> int i, assert_equal(int, int); #define test_true(x, y) cr_assert(assert_equal((x), (y))) #define test_false(x, y) cr_assert_not(assert_equal((x), (y))) _Bool strtob(const char *s) { return strcmp(s, "true") ? 0 : 1; } Test(sample_cases, return_true) { int samples[6][2] = {{1, 1}, {0, 0}, {-1, -1}, {1, true}, {true, true}, {'1', '1'}}; for (i = 0; i < 6; i++) { test_true(samples[i][0], samples[i][1]); } } Test(more_sample_cases, return_false) { int more_samples[6][2] = {{1, 0}, {'1', 1}, {-1, strtob("true")}, {-1, 1}, {false, true}, {0, true}}; for (i = 0; i < 6; i++) { test_false(more_samples[i][0], more_samples[i][1]); } }
import unittestfrom solution import assert_equal- #include <criterion/criterion.h>
- #include <string.h>
class TestAssertEqual(unittest.TestCase):def setUp(self) -> None:self.assert_true = ((1, 1),(0, 0),(-1, -1),(1, True),(True, True),('1', '1'),)self.assert_false = ((1, 0),('1', 1,),(1, 'True',),(-1, 1,),(False, True,),(0, True,),)- int i, assert_equal(int, int);
def test_assert_true(self):for p1, p2 in self.assert_true:self.assertEqual(assert_equal(p1, p2), None)- #define test_true(x, y) cr_assert(assert_equal((x), (y)))
- #define test_false(x, y) cr_assert_not(assert_equal((x), (y)))
def test_assert_false(self):for p1, p2 in self.assert_false:self.assertRaises(AssertionError, assert_equal, p1, p2)- _Bool strtob(const char *s) {
- return strcmp(s, "true") ? 0 : 1;
- }
- Test(sample_cases, return_true) {
- int samples[6][2] = {{1, 1}, {0, 0}, {-1, -1}, {1, true}, {true, true}, {'1', '1'}};
- for (i = 0; i < 6; i++) {
- test_true(samples[i][0], samples[i][1]);
- }
- }
if __name__ == '__main__':unittest.main()- Test(more_sample_cases, return_false) {
- int more_samples[6][2] = {{1, 0}, {'1', 1}, {-1, strtob("true")}, {-1, 1}, {false, true}, {0, true}};
- for (i = 0; i < 6; i++) {
- test_false(more_samples[i][0], more_samples[i][1]);
- }
- }
section .text global factorial ; RAX factorial (RDI n) factorial: mov rax, 1 mov rsi, 1 cmp rsi, rdi jle multiply ret multiply: mul rsi inc rsi cmp rsi, rdi jle multiply ret
def factorial(n: int) -> int:"""Returns factorial of a non-negative integer."""return 1 if n <= 0 else eval('*'.join([str(i) for i in range(1, n + 1)]))- section .text
- global factorial ; RAX factorial (RDI n)
- factorial:
- mov rax, 1
- mov rsi, 1
- cmp rsi, rdi
- jle multiply
- ret
- multiply:
- mul rsi
- inc rsi
- cmp rsi, rdi
- jle multiply
- ret
#include <criterion/criterion.h> unsigned long long factorial(int); Test(sample_cases, print_all_factorials_below_ULLONG_MAX) { unsigned long long factorials[23] = {1}; cr_assert_eq(*factorials, 1); for (int i = 1; i < 23; i++) { cr_assert_eq(factorial(i), factorials[i] = i * factorials[i - 1]); } }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- #include <criterion/criterion.h>
# test.assert_equals(actual, expected, [optional] message)test.assert_equals(factorial(-1), 1)test.assert_equals(factorial(0), 1)test.assert_equals(factorial(1), 1)test.assert_equals(factorial(2), 2)test.assert_equals(factorial(3), 6)test.assert_equals(factorial(4), 24)test.assert_equals(factorial(5), 120)test.assert_equals(factorial(6), 720)test.assert_equals(factorial(7), 5040)test.assert_equals(factorial(8), 40320)test.assert_equals(factorial(9), 362880)test.assert_equals(factorial(10), 3628800)- unsigned long long factorial(int);
- Test(sample_cases, print_all_factorials_below_ULLONG_MAX) {
- unsigned long long factorials[23] = {1};
- cr_assert_eq(*factorials, 1);
- for (int i = 1; i < 23; i++) {
- cr_assert_eq(factorial(i), factorials[i] = i * factorials[i - 1]);
- }
- }
const long div2 = 55143606028634440;
unsigned long long div2(unsigned long long a){unsigned long long ans{};while (ans * 2 + 1 < a) ans++;return ans;}- const long div2 = 55143606028634440;
#include <criterion/criterion.h> #define test_div2(x, y) cr_assert_eq(div2((x)), (y)) unsigned long long div2(unsigned long long); Test(sample_cases, smaller_numbers) { test_div2(~0 + 1, 0); test_div2(3, 1); test_div2(10, 5); } Test(more_sample_cases, larger_numbers) { test_div2(65536, 32768); test_div2(6553665535, 3276832767); test_div2(0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF); }
// TODO: Replace examples and use TDD by writing your own tests- #include <criterion/criterion.h>
Describe(d2){It(under10){Assert::That(div2(10), Equals(5));Assert::That(div2(0), Equals(0));Assert::That(div2(3), Equals(1));}It(huge_numbers){Assert::That(div2(65536), Equals(32768));Assert::That(div2(6553665535), Equals(3276832767));Assert::That(div2(1234567890), Equals(617283945));}};- #define test_div2(x, y) cr_assert_eq(div2((x)), (y))
- unsigned long long div2(unsigned long long);
- Test(sample_cases, smaller_numbers) {
- test_div2(~0 + 1, 0);
- test_div2(3, 1);
- test_div2(10, 5);
- }
- Test(more_sample_cases, larger_numbers) {
- test_div2(65536, 32768);
- test_div2(6553665535, 3276832767);
- test_div2(0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF);
- }
Now with full (2 ** 3 / 2 ** 3)
coverage testing!
#include <stdbool.h> bool bool_check(bool bools[3]) { return *bools + *++bools + *++bools > 1; }
public class Kumite {public static boolean boolCheck(boolean[] bools) {return bools[0] ? bools[1] || bools[2] : bools[1] && bools[2];}- #include <stdbool.h>
- bool bool_check(bool bools[3]) {
- return *bools + *++bools + *++bools > 1;
- }
#include <criterion/criterion.h> bool bool_check(bool []); Test(sample_cases, two_are_true) { cr_assert_eq(bool_check((bool []) {false, true, true}), true); cr_assert_eq(bool_check((bool []) {true, false, true}), true); cr_assert_eq(bool_check((bool []) {true, true, false}), true); } Test(another_case, all_are_true) { cr_assert_eq(bool_check((bool []) {true, true, true}), true); } Test(more_sample_cases, one_is_true) { cr_assert_eq(bool_check((bool []) {true, false, false}), false); cr_assert_eq(bool_check((bool []) {false, true, false}), false); cr_assert_eq(bool_check((bool []) {false, false, true}), false); } Test(last_case, all_are_false) { cr_assert_eq(bool_check((bool []) {false, false, false}), false); }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- #include <criterion/criterion.h>
// TODO: Replace examples and use TDD by writing your own tests- bool bool_check(bool []);
class SolutionTest {@Testvoid test0True() {boolean[] bools = new boolean[3];assertEquals(false, Kumite.boolCheck(bools));}@Testvoid test1True() {boolean[] bools = new boolean[3];for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {bools[j] = false;}bools[i] = true;assertEquals(false, Kumite.boolCheck(bools));}}@Testvoid test2True() {boolean[] bools = new boolean[3];for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {bools[j] = true;}bools[i] = false;assertEquals(true, Kumite.boolCheck(bools));}}@Testvoid test3True() {boolean[] bools = new boolean[]{true, true, true};assertEquals(true, Kumite.boolCheck(bools));}- Test(sample_cases, two_are_true) {
- cr_assert_eq(bool_check((bool []) {false, true, true}), true);
- cr_assert_eq(bool_check((bool []) {true, false, true}), true);
- cr_assert_eq(bool_check((bool []) {true, true, false}), true);
- }
- Test(another_case, all_are_true) {
- cr_assert_eq(bool_check((bool []) {true, true, true}), true);
- }
- Test(more_sample_cases, one_is_true) {
- cr_assert_eq(bool_check((bool []) {true, false, false}), false);
- cr_assert_eq(bool_check((bool []) {false, true, false}), false);
- cr_assert_eq(bool_check((bool []) {false, false, true}), false);
- }
- Test(last_case, all_are_false) {
- cr_assert_eq(bool_check((bool []) {false, false, false}), false);
- }