Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.
You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.
A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.
PHP Functions - Passing Variables by Reference
Normally, in PHP, variables are passed into functions by value only. In most cases, this is considered convenient for the (PHP) developer because the developer can actively modify the value passed into the function without having to worry that it might alter the value of the original variable being passed in:
// The Function
function foo($number) {
$number = $number ** 2; // Here, I actively modify the value passed in before returning the result.
return $number;
}
// The Original Variable
$my_var = 6;
// The New Variable (created by passing the original variable into function 'foo')
$my_new_var = foo($my_var);
// Now let's see what has happened to both variables
echo "$my_var\n"; // 6 (The value of the original variable remains the same after being passed into the function)
echo "$my_new_var\n"; // 36 (as expected)
In other languages such as Javascript, actively modifying the parameter passed in would alter the value of the original variable.
However, this behaviour may not always be desirable. Suppose we wanted to actively modify the variable passed in (for whatever reason). Then we could specify in the function definition that the variable is to be passed by reference. By doing so, if the parameter inside the function is assigned a new value, it will modify the original variable.
E.g.
// The Functions
function increment(&$number) {
// In PHP, the ampersand is used before the parameter name to specify that the variable is to be passed in by reference, not by value
$number++;
}
function decrement(&$number) {
$number--;
}
// The Variable
$a = 10;
increment($a);
echo "$a\n"; // 11
increment($a);
echo "$a\n"; // 12
decrement($a);
decrement($a);
decrement($a);
echo "$a\n"; // 9
// The Functions
function increment(&$number) {
// In PHP, the ampersand is used before the parameter name to specify that the variable is to be passed in by reference, not by value
$number++;
}
function decrement(&$number) {
$number--;
}
function twice(&$number) {
$number = $number * 2;
}
function halve(&$number) {
$number = $number / 2;
}
// The Variable
$a = 10;
increment($a);
echo "$a\n"; // 11
increment($a);
echo "$a\n"; // 12
decrement($a);
decrement($a);
decrement($a);
echo "$a\n"; // 9
twice($a);
twice($a);
echo "$a\n"; // 36
halve($a);
echo "$a\n"; // 18
Code from the official page
class MyTestCase extends GroovyTestCase {
void testAssertions() {
assertTrue(1 == 1)
assertEquals("test", "test")
def x = "42"
assertNotNull "x must not be null", x
assertNull null
assertSame x, x
}
}
pow => powerOf => x^y
A simple function I use all the time because I cannot be bothered to remember how to use Math.Pow(). Also, who uses double anyway? Pshh, real men use BigInteger.
using System.Numerics;
public class basic
{
public static BigInteger pow(long down, long up)
{
BigInteger power = 1;
for (long i = 0; i < up; i++)
power *= down;
return power;
}
}
namespace Solution
{
using NUnit.Framework;
using System;
using System.Numerics;
[TestFixture]
public class SolutionTest
{
[Test] public void Test01_ZeroZero() {Assert.AreEqual(BigInteger.Parse("1"),basic.pow(0,0));}
[Test] public void Test02_OneZero() {Assert.AreEqual(BigInteger.Parse("1"),basic.pow(1,0));}
[Test] public void Test03_OneOne() {Assert.AreEqual(BigInteger.Parse("1"),basic.pow(1,1));}
[Test] public void Test04_TwoFive() {Assert.AreEqual(BigInteger.Parse("32"),basic.pow(2,5));}
[Test] public void Test05_ThreeSix() {Assert.AreEqual(BigInteger.Parse("729"),basic.pow(3,6));}
[Test] public void Test06_NineNine() {Assert.AreEqual(BigInteger.Parse("387420489"),basic.pow(9,9));}
[Test] public void Test07_FiftyFifty() {Assert.AreEqual(BigInteger.Parse("8881784197001252323389053344726562500000000000000000000000000000000000000000000000000"),basic.pow(50,50));}
[Test] public void Test08_NinetyNinety() {Assert.AreEqual(BigInteger.Parse("76177348045866392339289727720615561750424801402395196724001565744957137343033038019601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),basic.pow(90,90));}
[Test] public void Test09_NinetyThousand() {Assert.AreEqual(BigInteger.Parse("17478712517226516096599746191646605705290624874351885178118880118106862662272754892914864698646811110756089506961452765887713684358755086475144142020936384818729123800899771793815296284783205235193191426815044240594108902145005006478139358189257019054026054840981379569793685510258252394113186439979165236770447696626286464065403356279753296192642450797504708624624740911054444373553021461514753480907553301532690679330916994798890898246508417955674786063969756645571437376570270804032399777578652968467400937123779157705360942236880491080232441391830279624844110784644395168452279619352212698147534167825764555073160737519853740460645925467960431507378083145016846797580569059487592463686444161518631380852766035958164109451575997420776176189116011851556020807717467859593598794901919333899652712754031279254322479632696759126461031563439543754427926889360470415335375231379413106908339497677642900813339003803104061547231578821124499916738190541104400010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),basic.pow(90,1000));}
[Test] public void Test10_Million1Thousand1() {Assert.AreEqual(BigInteger.Parse("1001001500666708091584834994213335499316592799502565312583810540758323526911111005634603686669483930857286743754321826864232849418408152954545886264688183157446039791353132425915857008553225953691126792159279415997411004085142266875746137278601021131587158113868597203639654409708386518036416338126344879480116985478676639672346205866699985617039342206813696096706810170372026388963501452270054596444893775441711368157252540330225398329021858809616465044877202437696122829981939260381511865042817499364770669626131656390789928416753321582790621270461663145953182133120159508842586845437332517739774364686813382854131224766878007967286105907099220085280974771001248345281733881815087444582493815359248506399809081455262963218787085971796708784417819709078423903187812206023309677068140671250652909082330966044887996637191644643001870302319446016321894880163091343763655640028627348026712035400168552662422697703087025675971958649891619972332396936779544869629925639642373670755026613785383804881927725955840710845268347727528204377517014451090398856164901406054457183440628124007521515977494906608533538720351729593325872484325368051858401047918905114231318807933763956168773876403630024179473024388226471743723242357056484831047288317527004682585284367156736997972373463352304570926594507764738886718885326634736917706033244678868941980403969141344332066224975739325910854819361961089332061508824940699925326248384186206668892457420714484048177404667198566346782352881670693363356026137100036086857141727005697917953869874510968868018248484064394619071158784988536685221274504268809514743110609402092691517062211247889071133236127462240006097676657804221497058466242982856433715137618975181171442055892128025984140957529945934947580429367312164463215067684695403656822567185772626021450217713080807091703806599434756198984073593789235750865250076955922907862149091151582477906606822061365808922685007970125026827181861447973883348081660930421996213112673271225609243103633310938058449682798195874948680996325465822637699703401778295647585449510161993711316893284253715410839571448646984603994892766639456740927472227613895849854062286515025190447270594059459534461048176276690661491781546540883134519133026079582791853176972560687419707095890773598047052969230399074811688006849486486033296269514427185928906242462789935768827391657448428036897005001657047045856507923647589800995458560931583149390873863211825046851722424886202035465138084799836109844679611489547814741722522514390227230190324651828594598004213598533984362761302413287642776029054834928861570146925209813386829813432836494084882274545988479012735939428163737844542565419279296476195556264651652218020995996003421928128958741067325661359124443276797727774152700246674799545789954731317039218565356236307758432749093754024228019549667790796622235945328608940970142858262177939426123016714310959922364772551492172161899369409188169046873995093320154742139253735820107313761476979274835663610003400393910964736902069862589090207895935256347134118293222818147392020957311239990457555709421060799403429886606172974212004975168681185320799027473448610272316951970666805401235379085666801651919020183004208337548656020151081217467639293789034418776108098220205693082390253105964376566416872433878437921064957486310206717418847701947881161707979395440406394984531218315784213741660332357989635433072469054645314118627108438799299246465189552695071262472675474795958382795444287765929081061044283303836480501817309065136071747767810234950340515765940305017485576522285599871638637272956997453125522645349137778590755296893045837680045826743749161245281838413738604501010326583530787852525869989843978729649205849089461694173231903810650656069896007845191179266759564500656091042888274860228698830512844581442441333669399444971861597929937022501943469451146555380507606110063020637644008716181415089397309743512203166821496059939097526544652755876092104749685012449661783616548342589721117212985155534551037397056891233242319730852629685519619176318313425682437450926437429445422014104886406470837743469298102701651079695342603730690444567039368747829725573255224305666027252854405816616610364867590402328447517993955513395462796498693632162318745129150232692955396565979337001035797332778935795998854716972313617791327730551220342674881439660111907009591856110628857078036746434114739600513920538997113352801438135357663390323476113831697491426043523645996666063187674917654896196583749849408957972958404103163290152954097054604739867014163820206232736380006687978618250323999705212652085787644292654845445474344705588587690103824201195146653435173120067116289222406325664052986272163689804372677998363215942960888420938085738469709186515919182253608885040402809608977237094680900522478635861071348403538358828058609431845880142594087429591459531740117997769012985910607434346155540638303006507453586061022551322253205099968073603257073944008154634006768895142499636451786775112253058327726882296146611809327954024216147969970581215281296011572202359006983414114745750196263631961985615358986417774764132335952866351007929400836937286241074911192762907251057982113973205366794161730098710943049724792420066346944088731249435622488610205448742697765634324416659987330308388601193609655775103865763521733024729866289922869617374564960665738842830884067777819704839721288661110053161340541013277427894784652498016718643541743487249795738580853402703985231307609588063557383670044060471958178085259108652873080390569598586169666021022135984339403534226866143732693865943065734402559787515237069442472866924477711793137412608749874148870441570385555423489812411628903675847379344932656775579369911700489900356106346388586949853593313068998608385351447746911252777240923214493147538094122000280293686974104028381451280774289237646806136759745839759400283494154561874749974061949289938696757191494143697708043545766369956735639882535219357733186610276631445175225464190204819946113276351686130664723930007257208098533408416533291916666500500500001001000001"),basic.pow(1000001,1001));}
}
}
JSTester - A custom Javascript TDD Framework
My attempt at creating a custom TDD framework in Javascript that runs on any browser.
This project can also be found on GitHub.
var Test = {
// Key Properties
passes: 0,
fails: 0,
errors: 0,
output: "",
// Test Output Method
write: function (output) {
document.getElementsByTagName("body")[0].innerHTML += output;
},
// Random Output Method
randomNumber: function () {
return ~~(101 * Math.random());
},
randomToken: function (length) {
length = length || 10;
var token = "";
for (var i = 0; i < length; i++) {
token += "abcdefghijklmnopqrstuvwxyz0123456789".split("")[~~(36 * Math.random())];
}
return token;
},
// Spec Methods
describe: function (msg, fn) {
var uniqId = this.randomToken();
msg = msg || "The code to be tested";
this.passes = this.fails = this.errors = 0;
this.output = "<div id='console_" + uniqId + "' style='color:white;background-color:black;padding:10px;font-family:monospace'>";
this.output += "<strong>" + msg + "</strong>";
this.output += "<div id='describe_" + uniqId + "' style='margin-left:20px'>";
var start = new Date().getTime();
try {
fn();
} catch (e) {
this.errors++;
this.output += "<span style='color:red'>" + e + "</span>";
}
var dur = new Date().getTime() - start;
this.output += "</div>";
this.output += "<hr />";
this.output += "<span style='color:lime'>" + this.passes + " Passes</span><br />";
this.output += "<span style='color:red'>" + this.fails + " Fails</span><br />";
this.output += "<span style='color:red'>" + this.errors + " Errors</span><br />";
this.output += "Process took " + dur + "ms to complete<br />"
this.output += "</div>";
this.write(this.output + "<br />");
document.getElementById("console_" + uniqId).style.border = "5px solid " + (this.passes > 0 && this.fails === 0 && this.errors === 0 ? "lime" : "red");
},
it: function (msg, fn) {
msg = msg || "should pass the tests below";
this.output += "<strong>" + msg + "</strong>";
this.output += "<div style='margin-left:20px'>";
try {
fn();
} catch (e) {
this.errors++;
this.output += "<span style='color:red'>" + e + "</span>";
}
this.output += "</div>";
},
// Basic Test Method
expect: function (passed, msg, success) {
msg = msg || "Value was not what was expected";
success = success || "Test Passed";
if (passed) {
this.passes++;
this.output += "<span style='color:lime'>" + success + "</span><br />";
} else {
this.fails++;
this.output += "<span style='color:red'>" + msg + "</span><br />";
}
},
// Assertion Methods
assertEquals: function (actual, expected, msg, success) {
msg = (msg || "Actual value did not match Expected") + " - Expected: " + expected + ", but instead got: " + actual;
success = (success || "Test Passed") + " - Value === " + expected;
this.expect(actual === expected, msg, success);
},
assertNotEquals: function (actual, unexpected, msg, success) {
msg = (msg || "Unexpected value was returned") + " - Value was expected to not equal: " + unexpected;
success = (success || "Test Passed") + " - Value !== " + unexpected;
this.expect(actual !== unexpected, msg, success);
},
assertSimilar: function (actual, expected, msg, success) {
msg = (msg || "Actual value did not match Expected") + " - Expected: " + JSON.stringify(expected) + ", but instead got: " + JSON.stringify(actual);
success = (success || "Test Passed") + " - Value === " + JSON.stringify(expected);
this.expect(JSON.stringify(actual) === JSON.stringify(expected), msg, success);
},
assertNotSimilar: function (actual, unexpected, msg, success) {
msg = (msg || "Unexpected value was returned") + " - Value was expected to not equal: " + JSON.stringify(unexpected);
success = (success || "Test Passed") + " - Value !== " + JSON.stringify(unexpected);
this.expect(JSON.stringify(actual) !== JSON.stringify(unexpected), msg, success);
},
expectError: function (msg, fn, success) {
var errorThrown = !1;
msg = msg || "Expected error was not thrown";
success = success || "Test Passed";
try {
fn();
} catch (e) {
errorThrown = true;
this.output += e + "<br />";
} finally {
this.expect(errorThrown, msg, success);
}
},
expectNoError: function (msg, fn, success) {
var errorThrown = !1;
msg = msg || "Unexpected error thrown";
success = success || "Test Passed";
try {
fn();
} catch (e) {
errorThrown = true;
msg += " - " + e;
} finally {
this.expect(!errorThrown, msg, success);
}
}
};
Test.expect(true);
RubyTester - A custom Ruby TDD Framework
My attempt at creating a custom Ruby testing framework that executes in the command line. Can also be found on GitHub.
NOTE: Original class name was Test
but it clashed with the name of the testing framework used here at Codewars so had to rename it.
class RubyTester
def initialize
@passes = 0
@fails = 0
@errors = 0
end
def describe msg, &block
80.times do
print "#"
sleep 0.025
end
puts "\e[1mRubyTester\e[22m"
sleep 1
puts "\e[1mv1.0.0\e[22m"
sleep 1
puts "\e[1mAuthored by DonaldKellett\e[22m (https://github.com/DonaldKellett)"
sleep 1
@passes = 0
@fails = 0
@errors = 0
print "Tests will execute in 3"
sleep 1
print " 2"
sleep 1
puts " 1"
sleep 1
puts "-" * 80
puts "\e[1mDescribe: #{msg}\e[22m"
start = Time.now
begin
yield
rescue Exception => e
@errors += 1
puts "\e[31mError: #{e}\e[0m"
end
dur = ((Time.now - start) * 1000).round
puts "-" * 80
sleep 1
puts "\e[32m#{@passes} Passed\e[0m"
sleep 1
puts "\e[31m#{@fails} Failed\e[0m"
sleep 1
puts "\e[31m#{@errors} Errors\e[0m"
sleep 1
puts "Process took #{dur}ms to complete"
sleep 1
puts "\e[1mThank you for using RubyTester :D\e[22m"
puts "#" * 80
end
def it msg, &block
puts "\e[1mIt: #{msg}\e[22m"
begin
yield
rescue Exception => e
@errors += 1
puts "\e[31mError: #{e}\e[0m"
end
end
def expect passed, msg = "Value was not what was expected", success = "Test Passed"
if passed
@passes += 1
puts "\e[32m#{success}\e[0m"
else
@fails += 1
puts "\e[31m#{msg}\e[0m"
end
end
def assert_equals actual, expected, msg = "Actual value did not match expected", success = "Test Passed"
self.expect actual == expected, msg + " - Expected: " + expected.to_s + ", but instead got: " + actual.to_s, success + " - Value == " + expected.to_s
end
def assert_not_equals actual, unexpected, msg = "Unexpected value returned", success = "Test Passed"
self.expect actual != unexpected, msg + " - Value was expected to not equal: " + unexpected.to_s, success + " - Value != " + unexpected.to_s
end
def expect_error msg, &block
error_thrown = false
begin
yield
rescue Exception => e
error_thrown = true
puts "Expected error thrown: #{e}"
end
self.expect error_thrown, msg
end
def expect_no_error msg, &block
error_thrown = false
error_msg = "Error"
begin
yield
rescue Exception => e
error_thrown = true
error_msg = e
end
self.expect !error_thrown, "#{msg} - #{e}"
end
def random_number
rand 101
end
def random_token length = 10
token_chars = "abcdefghijklmnopqrstuvwxyz0123456789".split ""
token = ""
length.times do
token += token_chars[(36 * rand).floor]
end
token
end
end
Test.expect true
let beerSongVerse n =
match n with
| 0 -> "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"
| 1 -> "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n"
| 2 -> "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n"
| _ -> System.String.Format("{0} bottles of beer on the wall, {0} bottles of beer.\nTake one down and pass it around, {1} bottles of beer on the wall.\n", n, n - 1)
let beerSong n m =
[m .. n] |> List.rev |> List.map(fun x -> beerSongVerse x) |> String.concat("\n")
beerSong 99 0
let rec hello n =
match n with
| 0 -> ()
| _ -> System.Console.WriteLine("hello"); hello (n - 1)
let add a b = a + b
let square x = x * x
let result = add 1 2 |> add 3 |> add 4 |> square
printfn result
const fn = (f) => (...args) => args.reduce((f, arg) => f(arg), f);
// ==================================
const sum = fn(a => b => c => (a + b + c));
console.log(sum(1, 2, 3)); // 6
console.log(sum(1, 2)(3)); // 6
console.log(sum(1)(2)(3)); // 6
const doubleInc = sum(1)(1);
console.log(doubleInc(4)); // 6
Taking a look at testing capabilities...
Printing arguments: work in progress...
#include <cstddef>
template <class C>
bool hasSize(C&& xs, std::size_t n) {
return xs.size() == n;
}
#include <vector>
Describe(igloo_framework) {
It(does_not_print_arguments) {
using vi = std::vector<int>;
Assert::That(hasSize(vi{0, 1, 2}, 3), Is().True());
}
};