#include <stdint.h> uint32_t sum(uint32_t a, uint32_t b) { if(a == 0u) { return b; } return sum(a-1, b+1); }
function sum(a,b) {return a+b; // wrong returning- #include <stdint.h>
- uint32_t sum(uint32_t a, uint32_t b)
- {
- if(a == 0u)
- {
- return b;
- }
- return sum(a-1, b+1);
- }
#include <criterion/criterion.h> #include <stdint.h> uint32_t sum(uint32_t a, uint32_t b); Test(the_sum_function, should_pass_all_the_tests_provided) { cr_assert_eq(sum(1, 1), 2); cr_assert_eq(sum(0, 50), 50); cr_assert_eq(sum(50, 100), 150); }
// Since Node 10, we're using Mocha.// You can use `chai` for assertions.const chai = require("chai");const assert = chai.assert;// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- #include <criterion/criterion.h>
- #include <stdint.h>
describe("Solution", function() {it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);assert.strictEqual(sum(1,1), 2);});});- uint32_t sum(uint32_t a, uint32_t b);
- Test(the_sum_function, should_pass_all_the_tests_provided) {
- cr_assert_eq(sum(1, 1), 2);
- cr_assert_eq(sum(0, 50), 50);
- cr_assert_eq(sum(50, 100), 150);
- }
Some additional robustness
#include <stddef.h> const char* digit_to_text(unsigned digit) { static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; if(digit > 9u) { return NULL; } return nums[digit]; }
const char *nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};- #include <stddef.h>
const char* digit_to_text(int digit) {return nums[digit];- const char* digit_to_text(unsigned digit)
- {
- static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
- if(digit > 9u)
- {
- return NULL;
- }
- return nums[digit];
- }
#include <criterion/criterion.h> const char* digit_to_text(int digit); Test(digit_to_text, should_pass_all_the_tests_provided) { cr_assert_str_eq(digit_to_text(0), "zero"); cr_assert_str_eq(digit_to_text(1), "one"); cr_assert_str_eq(digit_to_text(2), "two"); cr_assert_str_eq(digit_to_text(3), "three"); cr_assert_str_eq(digit_to_text(4), "four"); cr_assert_str_eq(digit_to_text(5), "five"); cr_assert_str_eq(digit_to_text(6), "six"); cr_assert_str_eq(digit_to_text(7), "seven"); cr_assert_str_eq(digit_to_text(8), "eight"); cr_assert_str_eq(digit_to_text(9), "nine"); cr_assert_eq(digit_to_text(10), NULL); }
- #include <criterion/criterion.h>
- const char* digit_to_text(int digit);
Test(the_multiply_function, should_pass_all_the_tests_provided) {cr_assert_str_eq(digit_to_text(3), "three");cr_assert_str_eq(digit_to_text(9), "nine");- Test(digit_to_text, should_pass_all_the_tests_provided) {
- cr_assert_str_eq(digit_to_text(0), "zero");
- cr_assert_str_eq(digit_to_text(1), "one");
- cr_assert_str_eq(digit_to_text(2), "two");
- cr_assert_str_eq(digit_to_text(3), "three");
- cr_assert_str_eq(digit_to_text(4), "four");
- cr_assert_str_eq(digit_to_text(5), "five");
- cr_assert_str_eq(digit_to_text(6), "six");
- cr_assert_str_eq(digit_to_text(7), "seven");
- cr_assert_str_eq(digit_to_text(8), "eight");
- cr_assert_str_eq(digit_to_text(9), "nine");
- cr_assert_eq(digit_to_text(10), NULL);
- }
Now in C!
#include <stdint.h> #include <stdbool.h> #define MAX_ANGLE 180u typedef enum { ACUTE, RIGHT, OBTUSE, } AngleType; typedef enum { EQUILATERAL, ISOSCELES, SCALENE, } SideType; typedef struct { union { struct { uint32_t a; uint32_t b; uint32_t c; }; uint32_t angles[3]; }; } Triangle; bool TriangleIsValid(const Triangle* triangle); AngleType TriangleGetAngleType(const Triangle* triangle); SideType TriangleGetSideType(const Triangle* triangle); uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2); bool TriangleIsValid(const Triangle* triangle) { return triangle->a + triangle->b + triangle->c == 180u; } AngleType TriangleGetAngleType(const Triangle* triangle) { for(unsigned i = 0u; i < 3u; i++) { if(triangle->angles[i] > 90u) { return OBTUSE; } if(triangle->angles[i] == 90u) { return RIGHT; } } return ACUTE; } SideType TriangleGetSideType(const Triangle* triangle) { uint32_t a = triangle->a; uint32_t b = triangle->b; uint32_t c = triangle->c; if(a == b && b == c) { return EQUILATERAL; } if(a == b || b == c || c == a) { return ISOSCELES; } return SCALENE; } uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2) { return 180u - angle1 - angle2; }
use std::marker::PhantomData;- #include <stdint.h>
- #include <stdbool.h>
#[derive(Debug, PartialEq, Eq)]enum AngleType {Acute,Right,Obtuse}#[derive(Debug, PartialEq, Eq)]enum SideType {Equilateral,Isoceles,Scalene}struct Complete;struct Incomplete;- #define MAX_ANGLE 180u
struct Triangle<T> {angles: [u32; 3],phantom: PhantomData<T>- typedef enum
- {
- ACUTE,
- RIGHT,
- OBTUSE,
- } AngleType;
- typedef enum
- {
- EQUILATERAL,
- ISOSCELES,
- SCALENE,
- } SideType;
- typedef struct
- {
- union
- {
- struct
- {
- uint32_t a;
- uint32_t b;
- uint32_t c;
- };
- uint32_t angles[3];
- };
- } Triangle;
- bool TriangleIsValid(const Triangle* triangle);
- AngleType TriangleGetAngleType(const Triangle* triangle);
- SideType TriangleGetSideType(const Triangle* triangle);
- uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
- bool TriangleIsValid(const Triangle* triangle)
- {
- return triangle->a + triangle->b + triangle->c == 180u;
- }
impl<T> Triangle<T> {fn angle_type(&self) -> AngleType {for angle in self.angles {if angle > 90 {return AngleType::Obtuse;}if angle == 90 {return AngleType::Right;}}AngleType::Acute}fn side_type(&self) -> SideType {let [a, b, c] = self.angles;if a == b && b == c {SideType::Equilateral} else if a == b || b == c || c == a {SideType::Isoceles} else {SideType::Scalene}}- AngleType TriangleGetAngleType(const Triangle* triangle)
- {
- for(unsigned i = 0u; i < 3u; i++)
- {
- if(triangle->angles[i] > 90u)
- {
- return OBTUSE;
- }
- if(triangle->angles[i] == 90u)
- {
- return RIGHT;
- }
- }
- return ACUTE;
- }
impl Triangle<Complete> {fn new(a: u32, b: u32, c: u32) -> Self {Self { angles: [a, b, c], phantom: PhantomData::<Complete> }}- SideType TriangleGetSideType(const Triangle* triangle)
- {
- uint32_t a = triangle->a;
- uint32_t b = triangle->b;
- uint32_t c = triangle->c;
- if(a == b && b == c)
- {
- return EQUILATERAL;
- }
- if(a == b || b == c || c == a)
- {
- return ISOSCELES;
- }
- return SCALENE;
- }
impl Triangle<Incomplete> {fn new(a: u32, b: u32) -> Self {Self { angles: [a, b, 180 - a - b], phantom: PhantomData::<Incomplete> }}fn other_angle(&self) -> u32 {self.angles[2]}- uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2)
- {
- return 180u - angle1 - angle2;
- }
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example. #include <criterion/criterion.h> #include <stdint.h> #include <stdbool.h> typedef enum { ACUTE, RIGHT, OBTUSE, } AngleType; typedef enum { EQUILATERAL, ISOSCELES, SCALENE, } SideType; typedef struct { union { struct { uint32_t a; uint32_t b; uint32_t c; }; uint32_t angles[3]; }; } Triangle; bool TriangleIsValid(const Triangle* triangle); AngleType TriangleGetAngleType(const Triangle* triangle); SideType TriangleGetSideType(const Triangle* triangle); uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2); Test(TriangleIsValid, should_pass_all_the_tests_provided) { Triangle good_triangle = {.angles = {60u, 60u, 60u}}; Triangle bad_triangle = {.angles = {90u, 50u, 50u}}; cr_assert_eq(TriangleIsValid(&good_triangle), true); cr_assert_eq(TriangleIsValid(&bad_triangle), false); } Test(TriangleGetAngleType, should_pass_all_the_tests_provided) { Triangle acute_triangle = {.angles = {70u, 50u, 60u}}; Triangle right_triangle = {.angles = {45u, 45u, 90u}}; Triangle obtuse_triangle = {.angles = {120u, 20u, 40u}}; cr_assert_eq(TriangleGetAngleType(´_triangle), ACUTE); cr_assert_eq(TriangleGetAngleType(&right_triangle), RIGHT); cr_assert_eq(TriangleGetAngleType(&obtuse_triangle), OBTUSE); } Test(TriangleGetSideType, should_pass_all_the_tests_provided) { Triangle equilateral_triangle = {.angles = {60u, 60u, 60u}}; Triangle isosceles_triangle = {.angles = {45u, 45u, 90u}}; Triangle scalene_triangle = {.angles = {120u, 20u, 40u}}; cr_assert_eq(TriangleGetSideType(&equilateral_triangle), EQUILATERAL); cr_assert_eq(TriangleGetSideType(&isosceles_triangle), ISOSCELES); cr_assert_eq(TriangleGetSideType(&scalene_triangle), SCALENE); } Test(TriangleGetThirdAngle, should_pass_all_the_tests_provided) { cr_assert_eq(TriangleGetThirdAngle(120u, 53u), 7u); cr_assert_eq(TriangleGetThirdAngle(21u, 62u), 97u); }
#[test]fn test() {let triangle1 = Triangle::<Incomplete>::new(30, 60);let triangle2 = Triangle::<Complete>::new(60, 60, 60);assert_eq!(triangle1.other_angle(), 90);// assert_eq!(triangle2.other_angle(), 90); // does not compile because other angle is already knownassert_eq!(triangle1.angle_type(), AngleType::Right);assert_eq!(triangle2.angle_type(), AngleType::Acute);assert_eq!(triangle1.side_type(), SideType::Scalene);assert_eq!(triangle2.side_type(), SideType::Equilateral);- // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
- #include <criterion/criterion.h>
- #include <stdint.h>
- #include <stdbool.h>
- typedef enum
- {
- ACUTE,
- RIGHT,
- OBTUSE,
- } AngleType;
- typedef enum
- {
- EQUILATERAL,
- ISOSCELES,
- SCALENE,
- } SideType;
- typedef struct
- {
- union
- {
- struct
- {
- uint32_t a;
- uint32_t b;
- uint32_t c;
- };
- uint32_t angles[3];
- };
- } Triangle;
- bool TriangleIsValid(const Triangle* triangle);
- AngleType TriangleGetAngleType(const Triangle* triangle);
- SideType TriangleGetSideType(const Triangle* triangle);
- uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
- Test(TriangleIsValid, should_pass_all_the_tests_provided) {
- Triangle good_triangle = {.angles = {60u, 60u, 60u}};
- Triangle bad_triangle = {.angles = {90u, 50u, 50u}};
- cr_assert_eq(TriangleIsValid(&good_triangle), true);
- cr_assert_eq(TriangleIsValid(&bad_triangle), false);
- }
- Test(TriangleGetAngleType, should_pass_all_the_tests_provided) {
- Triangle acute_triangle = {.angles = {70u, 50u, 60u}};
- Triangle right_triangle = {.angles = {45u, 45u, 90u}};
- Triangle obtuse_triangle = {.angles = {120u, 20u, 40u}};
- cr_assert_eq(TriangleGetAngleType(´_triangle), ACUTE);
- cr_assert_eq(TriangleGetAngleType(&right_triangle), RIGHT);
- cr_assert_eq(TriangleGetAngleType(&obtuse_triangle), OBTUSE);
- }
- Test(TriangleGetSideType, should_pass_all_the_tests_provided) {
- Triangle equilateral_triangle = {.angles = {60u, 60u, 60u}};
- Triangle isosceles_triangle = {.angles = {45u, 45u, 90u}};
- Triangle scalene_triangle = {.angles = {120u, 20u, 40u}};
- cr_assert_eq(TriangleGetSideType(&equilateral_triangle), EQUILATERAL);
- cr_assert_eq(TriangleGetSideType(&isosceles_triangle), ISOSCELES);
- cr_assert_eq(TriangleGetSideType(&scalene_triangle), SCALENE);
- }
- Test(TriangleGetThirdAngle, should_pass_all_the_tests_provided) {
- cr_assert_eq(TriangleGetThirdAngle(120u, 53u), 7u);
- cr_assert_eq(TriangleGetThirdAngle(21u, 62u), 97u);
- }