problem with `uvm_do_with: can't generate more data - uvm

I met a problem with data constraint in UVM `uvm_do_with. I have a piece of code like this:
a) first I defined a data item:
class eth_item_data extends uvm_sequence_item;
...
rand int tx_len;
rand logic [7:0] txd[0:1500];
...
constraint txd_range_c { foreach (txd[i]) txd[i] <=255;
foreach (txd[i]) txd[i] >=0;
}
endclass
b) then I generated the data in the sequence:
class base_seq extends uvm_sequence #(eth_item_data);
...
virtual task body();
...
data_obj = eth_item_data::type_id::create("data_obj");
`uvm_do_with (data_obj, {...
txd[0][7:0]==8'h11;
txd[1][7:0]==8'h22;
txd[2][7:0]==8'h33;
txd[3][7:0]==8'h44;
...
txd[109][7:0]==8'hff;
}
)
endtask
endclass
It is very strange that:
Q1): With code above it will shows randomize method call failed. The simulator implies `uvm_do_with constraint conflicts with these:
These variables contribute to the set of conflicting constraints:
rand variables:
txd[1501] ......
Q2): if I only generate data with a small number, such as
`uvm_do_with (data_obj, {...
txd[0][7:0]==8'h11;
txd[1][7:0]==8'h22;
txd[2][7:0]==8'h33;
txd[3][7:0]==8'h44;
txd[4][7:0]==8'h55;
txd[5][7:0]==8'h66;
txd[6][7:0]==8'h77;
txd[7][7:0]==8'h88;
}
)
data could be generated. This confused me. Can anybody help me to understand why I can't generate data with more? thanks.

I rewrite the test word by word and then I re-compiled the code. This time there is no issues. I guess this problem is caused by some "hidden typo" in the source which doesn't show, such as some control characters. Thanks for those comments, which give me a reminder.

Related

TestCafe loop over elements and check if class exists within Set

I have a table with a column of icons. Each Icon has a class of "test" and then "test" + [some rating]. The rating could be A, B, C, XX, YY. I'd like to select the group of icons and loop over them, pop off the last class (the one with the rating) and then expect that my Set of classConsts contains the class in question. I've done a bunch of research but can only find examples of interacting with each of the elements, I'm not sure what I'm doing wrong when trying to check the classes on each instead. Any help is appreciated, thanks.
The below code blows up when i call mrArray.nth saying it's not a function (sorry its a bit messy, had to change some variable names around)
test('Sers are verified', async t => {
const IconArr = Selector('#testtable .test');
var count = await IconArr().count;
var myArray = await IconArr();
const classConsts = ["testClassA", "testClassB", "testClassC", "testClassXX", "testClassYY"]
let mySet = new Set(classConsts);
for(let i = 1; i <= count; i++){
console.log(mySet.has(myArray.nth(i).classNames.pop()));
};
});
myArray is no longer a Selector object after you execute it as an asynchronous function. Please refer to the following help article for more information DOMNodeState.
Also, the index starts with 0, not 1. Your code may look as follows:
for(let i = 0; i \< count; i++){
let classes = await IconArr().nth(i).classNames;
console.log(mySet.has(classes.pop()));
};
You can always debug your test cases to see what goes wrong:
https://devexpress.github.io/testcafe/documentation/recipes/debugging/chrome-dev-tools.html

Why this code is generating array index out of bound?

I am stuck for past 5 - 6 hours in figuring this out that why this code is generating array index out of bound error on run time. I am unable to find out the reason. Can you please tell what modifications are required to correct this code?
spotsArr := make(map[int][]map[int64][]int)
for ind, availableSpot := range availableSpots {
spotsArr[availableSpot.Uid][ind] = make(map[int64][]int)
spotsArr[availableSpot.Uid][ind][availableSpot.Date] = []int{availableSpot.SpotSlug}
}
fmt.Println(spotsArr)
Edit 1: View the full code here https://play.golang.org/p/Smm0BFgtNp
Edit 2: Actually what I need to do is to get output in format something like:
{ uid: { date: {spot_slug, spot_slug} } }
{ 86: { 1536710400: {1000, 1200, 900},
{ 1536105600: {900} } }
The error is, as the error message suggests, because you tried to assign element on the index greater than the slice length. For the sake of getting the error away, you can just initialize the slice to the length, at least, as much as the index you wanted to use :
....
spotsArr[availableSpot.Uid] = make([]map[int64][]int, ind+1, ind+1)
spotsArr[availableSpot.Uid][ind] = make(map[int64][]int)
....
But as you clarified further about the desired output, it seems that you don't need slice in the first place. You need map of Uid where each key has value of map of Date :
spotsArr := make(map[int]map[int64][]int)
for _, availableSpot := range availableSpots {
if _, ok := spotsArr[availableSpot.Uid]; !ok {
spotsArr[availableSpot.Uid] = make(map[int64][]int)
}
spotsArr[availableSpot.Uid][availableSpot.Date] = append(spotsArr[availableSpot.Uid][availableSpot.Date],availableSpot.SpotSlug)
}
fmt.Println(spotsArr)
playground
Given the last two data have the same date, the output is as follows :
map[86:map[1534896000:[900] 1535500800:[900] 1536105600:[900] 1537315200:[900 900]]]
spotsArr is a map of int to an array of maps - map[int][]...
spotsArr := make(map[int][]map[int64][]int)
On this line, you try to assign to an index of that array which has no members yet:
spotsArr[availableSpot.Uid][ind] = make(map[int64][]int)
You're saying set this spot availableSpot.Uid to something (fine) but then set the index ind in an array which doesn't have members to something else (not fine). To fix this I'd recommend trying to do less on each line so that it's much clearer where and what the problem is. You could do this to fix the grammar error:
spotsArr[availableSpot.Uid] = []map[int64][]int{make(map[int64][]int)}
But I can't think why you want to set an index on the map to the index of the Uids you're traversing (your code doing [Ind]). I'd try to make this less complex and confusing if you can and spread it out on several lines to make the intent clear.
PS Give people a code sample which runs (i.e. include all the structs used), it makes it easier to help.
PPS Thanks for code sample, that makes it clearer.

get classOf[Array[A]] from classOf[A]

I have a list of scala classes in the form like:
List(classOf[A], classOf[B], ...)
I need to register these classes as well as the array of the classes into kryo. The result is like:
kryo.register(classOf[A])
kryo.register(classOf[Array[A]])
kryo.register(classOf[B])
kryo.register(classOf[Array[B]])
...
So, with the list at hand, I may just use a foreach to register both the class and the array of the class.
However, I fail to get classOf[Array[A]] from classOf[A]. I have tried the ClassTag as following method:
def getArrayClass[T: ClassTag](c: Class[T]): Class[_] = {
classOf[Array[T]]
}
The result is not the right (though the type of the both results is the same), and kryo still complains that Class is not registered: A[].
scala> getArrayClass(classOf[A])
res0: Class[Array[A]] = class java.lang.Object
scala> classOf[Array[A]]
res1: Class[Array[A]] = class [LA;
Any clues? Thanks.
You can do it using the wrap method ClassTag:
def getArrayClass(c: Class[_]): Class[_] =
scala.reflect.ClassTag(c).wrap.runtimeClass
Note that this does not work if c is classOf[Null] or classOf[Nothing] (I believe this is bug in ClassTags). For any other Class, it will work.
You can also go to the Java way of doing it, without ClassTags, which is basically equivalent:
def getArrayClass(c: Class[_]): Class[_] =
java.lang.reflect.Array.newInstance(c, 0).getClass

How to iterate over a list of type Class to edit the properties of its objects in Groovy

I know there are more elaborate ways to achieve this in Java, but Groovy should have a concise way to do the same as per http://groovy.codehaus.org/Looping
Class Currency.groovy
class Currency {
String name
double rate
}
CurrencyController
def select(){
List<Currency> selectedCurrencies = Currency.getAll(params.currencies)
selectedCurrencies.eachWithIndex { obj, i -> obj.rate = update(obj.name)};
[selectedCurrencies:selectedCurrencies]
}
def update(String sym){
return sym
}
The above code throws:
No signature of method: currencychecker.CurrencyController$_$tt__select_closure12.doCall() is applicable for argument types: (currencychecker.Currency)
Thanks to #dmahapatro, the issue was that I was using an iterator variable obj[i], even though obj itself is the iterated object. The rest is correct!
I experimented with selectCurrencies.each as well instead of selectCurrencies.eachWithIndex however the right one in this case is eachWithIndex

CAKEPHP create unique code on entries in database

ok sorry, wrote too little info here and i have done some research:
The question: What is the easiest way to write a model function that creates an unique code (5 characters) for each row in a cake model/table that doesnt already have a code?
//create a unique code
//set the random id length
function generateCode()
{
while($i > 0)
{
$random_id_length = 7;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));
//finally I take the first 7 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);
//check if the code is unique
$i = $this->checkCode($rnd_id);
}
return $rnd_id;
}
//check to see if the code isnt already in the database
function checkCode($code)
{
$conditions = array("uniqueCode" => $code));
$result = $this->Visitors->count('all', array('conditions' => $conditions));
return $result;
}
//check how many has a code
function check()
{
$conditions = array("NOT" => array ("uniqueCode" => null));
$result = $this->Visitors->count('all', array('conditions' => $conditions));
return $result;
}
function update()
{
//update until everyone has a code
while( $i > 0)
{
$this->generateCode()
// get next visitors that DONT have a code.
$conditions = array ("uniqueCode" => null));
$visitor = $this->Visitors->find('first', array('conditions' => $conditions));
$i = $this->check();
}
echo 'Done';
}
Cant make it work properly and it must be an easier way?
How about an id column? :]
If You want it to be like a hash thing, I think use this:
substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,;:!?.$/*-+&#_+;./*&?$-!,"), 0, 5);
and you shouldn't worry about checking whether it's unique. Or do it, so create a hash like that and try to find it in the table, if it's not in there - your good to go.
in your Model, you can do something like this
<?php
function beforeSave() {
if (empty($this->data[$this->alias]['id'])) {
$this->data[$this->alias]['id'] = base_convert(uniqid(), 16, 36);
}
return $this->data;
}
?>
This will generate a unique id of 10 chars. You can figure out how to create a unique id of 5 chars, maybe converting the string to a higher base, but I find it difficult because you'll have to make your own function, base_convert has a limit of 36
One thing to think about is the length of the code. A five character code, assuming you use upper and lower case characters in it, has 380,204,032 possible unique codes. If you create these codes truly randomly, you'll rather soon hit the point where you're creating duplicates. There's a 50% chance of collision after you have generated 22,959 codes.
So, since for each new code you need to check whether it already exists or not,
(BTW, a much more elegant check is this):
do {
$code = /* create code */;
} while (!$this->isUnique(array('code' => $code)));
over time you'll hit more and more already existing codes, slowing down the generation of each code, to the point where you may have to loop 100 or more times to find a unique code (99% chance of collision at only 59,177 codes).
If you're just talking about a few thousand records max, I wouldn't worry about it. If your dataset could/is supposed to grow beyond that though, you should think either about a different scheme for your codes (UUIDs come to mind), generate codes sequentially or pre-generate a list of all possible codes, shuffle it and use up codes one by one.
There are a gazillion ways to do this and all you have to do is google the appropriate words. I found it in the PHP manual, so that would be a good place to start.
To make it unique, it would be based on date and would incorporate some sort of hashing.
Do some searching, reading and learning.

Resources