How can I implement a regression test in bazel? - bazel

I have the following test target:
block_test (
name = "full_test",
block = ":block_name"
...
params = {
"PARAM1" : 1,
"PARAM2" : 2,
"PARAM3" : 3
}
)
And I have a struct which defines the possible values of each param:
params_options = {
"param1" : [1, 2, 34],
"param2" : [43, 2 ,54],
"param3" : [3, 5, 6]
}
I would like to have a single target that would run a target like block_test for every possible combination of parameters.
I thought about doing this by creating a macro which will declare a target for every possible combination of parameters, and finally a test target which will depend on those targets.
Is there any better approach? There may be thousands of combinations and so:
I'm afraid I'll get a big mess when querying the build.
I'm afraid that this isn't very performant, with regards to memory utilization.

You can generate a block_test for each set of parameters using list comprehension:
[block_test (
name = "full_test",
block = ":block_name"
...
params = p
) for p in [
{1, 2, 34},
{43, 2 ,54},
{3, 5, 6},
]]

Related

How to convert number words to int (like three to 3) using Dart

I want to convert number words (like one, two, three and so on) to int (like 1, 2, 3 etc) using Dart
You have to depend on machine learning library or pair every string with the respective number.
int convertStrToNum(String str) {
var number = <String, num>{'one': 1, ...};
return number[str];
}
Of course, machine learning might be the fastest and best way to do this, but I can't really help you there. So, here's an implementation that would work assuming the "number word" follows a certain format, up until 10. (You could implement a RegExp parser to make this easier, but that would get tricky).
int convStrToNum(String str) {
var oneten = <String, num> {
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
}
if (oneten.keys.contains(str)) {
return oneten[str];
}
}
int convStrToInt(String str) {
var list = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
];
return list.indexOf(str);
}
I just pushed a repo addressing this issue. It's open to open to build upon so feel free to contribute, it would really help improve the package. Heres the link https://github.com/michaelessiet/wordstonumbers.dart

Use max_by in Ruby to find the key for the maximum number in a hash

I need to find the category, within an array, that holds the "largest" weight. I define the weights in an environment variable:
CATEGORY_WEIGHTS = {
"small_standard": 0,
"large_standard": 1,
"small_oversize": 2,
"medium_oversize": 3,
"large_oversize": 4
}
In this example, the "largest" weighted category would be large_oversize.
The array that I'm checking looks like this:
categories = [
"small_oversize",
"large_standard",
"small_standard",
"large_oversize"
]
But when I do this, it doesn't return the correct value:
max_category = categories.max_by{ |cat| CATEGORY_WEIGHTS[cat] }
It returns small_oversize instead of large_oversize.
Where did I go astray?
You're using symbols in the hash, but you're trying to access it with strings. Change it to:
max_category = categories.max_by{|cat| CATEGORY_WEIGHTS[cat.to_sym]}
And here is how you can do it simpler:
category_weights = {
small_standard: 0,
large_standard: 1,
small_oversize: 2,
medium_oversize: 3,
large_oversize: 4
}
category = category_weights.max_by{|c, w| w}[0]

Packing/Unpacking arguments in Dart [duplicate]

This question already has answers here:
Creating function with variable number of arguments or parameters in Dart
(6 answers)
Closed 5 years ago.
Is there a way in Dart to pack/unpack arguments in a function (like Python for example) ?
For packing example, being able to declare a function like this :
packArguments(*listOfArguments, **mapOfArguments) {
listOfArguments.forEach((arg) => print(arg));
mapOfArguments.forEach((key, val) => print("$key => $val"));
}
And then doing this :
packArguments("I", "Put", "whatever", "I", "want, arg1: "A", arg2: 1);
Would display :
I
Put
whatever
I
want
arg1 => A
arg2 => 1
As for unpacking, being able to do something like that :
functionWithLotOfArgument(a, b, c, d, e, {aa, bb = null, cc = null}) {
// do stuff
}
var argList = [1, 2, 3, 4, 5];
var argMap = {"aa": "haha", bb: "baby"};
functionWithLotOfArgument(*argList, **argMap);
Related issue https://github.com/dart-lang/sdk/issues/29087
This is not currently supported, but you can very easily pack yourself, by passing a list & map:
void packArguments(List listOfArguments, Map mapOfArguments) {
listOfArguments.forEach((arg) => print(arg));
mapOfArguments.forEach((key, val) => print("$key => $val"));
}
void main() {
packArguments(['a', 3], {'arg1': 'a', 'arg2': 5});
}
https://dartpad.dartlang.org/98ed3a3b07a2cca049cde69ca50ca269

Create multidimentional array in swift with different datatypes

this is my first question here so forgive me if it is not very clear.
I am trying to create an array in swift that will store either arrays of arrays or an integer number.
The array is supposed to be a simple representation of the data I will be using which is basically a tree kind of data structure like so...
Array = [ [[ [2, 3] ], [ 1, 4 ], [ 2 ]],
[ 2 ], [[2, 5], [6, 1] ], 3 ]
In overall the arrays are the branches and the integers are the leaves
I've tried declaring them as optionals like so
var test2 = [[[Int]?]?]()
Or using typedef's but I still can't get it to work.
Also, it should be possible to add new leaves to any of the arrays
Here is a solution based on enum, first declare the enum:
enum Node
{
case leaf(Int)
case branch([Node])
}
You can now write things such as:
let x = Node.leaf(42)
let y = Node.branch([Node.leaf(42), Node.leaf(24)])
However this is going to become laborious very quickly. Fortunately Swift allows conversions from literals, so we add:
extension Node : ExpressibleByIntegerLiteral
{
init(integerLiteral value: Int)
{
self = .leaf(value)
}
}
extension Node : ExpressibleByArrayLiteral
{
init(arrayLiteral elements: Node...)
{
self = .branch(elements)
}
}
And with those added we can now write the above two let statements as:
let x : Node = 42
let y : Node = [42, 24]
which is nicer. However if we print(y) we get:
branch([Node.leaf(42), Node.leaf(24)])
If you wish to pretty print that you can add:
extension Node : CustomStringConvertible
{
var description : String
{
switch self
{
case .leaf(let value):
return value.description
case .branch(let branches):
return branches.description
}
}
}
And now print(y) gives you:
[42, 24]
Finally your example:
let w : Node = [[[[2, 3]], [1, 4], [2]], [2], [[2, 5], [6, 1]], 3]
which prints as:
[[[[2, 3]], [1, 4], [2]], [2], [[2, 5], [6, 1]], 3]
You'll want to complete the enum type, with predicates such as isLeaf etc., but that is the basic idea.
HTH

How to 'unpack' table into function arguments

I'm trying to call a function in Lua that accepts multiple 'number' arguments
function addShape(x1, y1, x2, y2 ... xn, yn)
and I have a table of values which I'd like to pass as arguments
values = {1, 1, 2, 2, 3, 3}
Is it possible to dynamically 'unpack' (I'm not sure if this is the right term) these values in the function call? Something like..
object:addShape(table.unpack(values))
Equivalent to calling:
object:addShape(1, 1, 2, 2, 3, 3)
Apologies if this is a totally obvious Lua question, but I can't for the life of me find anything on the topic.
UPDATE
unpack(values) doesn't work either (delving into the method addShape(...) and checking the type of the value passed reveals that unpackis resulting in a single string.
You want this:
object:addShape(unpack(values))
See also: http://www.lua.org/pil/5.1.html
Here's a complete example:
shape = {
addShape = function(self, a, b, c)
print(a)
print(b)
print(c)
end
}
values = {1, 2, 3}
shape:addShape(unpack(values))
Whoever comes here and has Lua version > 5.1 unpack is moved into the table library so you can use: table.unpack
For more info: https://www.lua.org/manual/5.2/manual.html
This is not an answer about unpack, but a suggestion to use a different technique. Instead, do
function object:addShape(values)
for i,v in ipairs(values) do
x,y = v.x, v.y
...
end
end
function getPairs(values)
xyPairs = {}
for i=1,#values,2 do
v = {x=values[i], y=values[i+i] }
table.insert(xyPair, v)
end
return xyPairs
end
values = {1, 1, 2, 2, 3, 3}
object:addShape(getPairs(values))
The amount of work to be done should be similar as unpacking and the additional processing you will have to do in addShape() to support variable number of named arguments.

Resources