Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I compare two values in kdb/Q and output a message based on pass/fail?

I’m trying to build my own unit testing framework in Q, and what I’m trying to do is something like this:

// Is the expected value (EQUAL_TO/GREATER_THAN/LESS_THAN/ETC) the actual value?
// Object Object Object -> String
// checkExpect[5; =; 2+3] -> "Pass"
// checkExpect[1; =; 2+3] -> "Fail"

The function takes in 3 arguments, the expected output, the operator (‘=’, ‘>’, etc.), and the actual value, such as a function or expression.

I’m having trouble piecing those 3 together, and on top of that outputting either a "Pass" or "Fail" statement to see if the entire function passes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Apply x and z as arguments to y

The result boolean 0b and 1b can be used to index in to a list to choose either Pass or Fail

q)checkExpect:{("Fail";"Pass") y[x;z]}
q)checkExpect[5; =; 2+3]
"Pass"
q)checkExpect[1; =; 2+3]
"Fail"

https://code.kx.com/q/basics/function-notation/

x y z are special arguments you can skip naming

{[x;y;z] x+y+z} is the same as {x+y+z}

You might define your function with chosen names for clarity instead:

checkExpect:{[a;test;b] ("Fail";"Pass") test[a;b]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading