Why do my global variables don't get initialized in Test::Class? - unit-testing

Here is a simple test case using some global variables / constants:
use strict;
use warnings;
use base 'Test::Class';
use Test::More;
__PACKAGE__->runtests() unless caller;
my $ONE = "1";
my $TWO;
sub setup : Test(setup) {
$TWO = "2";
}
sub test_me : Tests {
is("1", $ONE);
is("2", $TWO);
}
When I run this, it fails because $ONE is not initialized:
robert#saaz:~$ prove test.t
test.t .. 1/?
# Failed test 'test me'
# at test.t line 16.
# (in main->test_me)
# got: '1'
# expected: undef
# Looks like you failed 1 test of 2.
test.t .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/2 subtests
Test Summary Report
-------------------
test.t (Wstat: 256 Tests: 2 Failed: 1)
Failed test: 1
Non-zero exit status: 1
Files=1, Tests=2, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.07 CPU)
Result: FAIL
Why is that? Clearly the variable is defined (or I'd get an error because of the use strict;), but it seems only initialization code in the setup function runs.
How do I use constants in Test::Class tests?

Your assignments do get executed, but only after you finish running the tests. This is because you run the tests (call __PACKAGE__->runtests()) before the assignments.
Furthermore, you're officially not allowed to use a variable before executing its my. Your arrangement is the only thing the docs call out as undefined behaviour (meaning it can result in any behaviour).

Related

Python: Match a special caracter with regular expression

Hi everyone I'm using the re.match function to extract pieces of string within a row from the file.
My code is as follows:
## fp_tmp => pointer of file
for x in fp_tmp:
try:
cpuOverall=re.match(r"(Overall CPU load average)\s+(\S+)(%)",x)
cpuUsed=re.match(r"(Total)\s+(\d+)(%)",x)
ramUsed=re.match(r"(RAM Utilization)\s+(\d+\%)",x)
####Not Work####
if cpuUsed is not None: cpuused_new=cpuUsed.group(2)
if ramUsed is not None: ramused_new=ramUsed.group(2)
if cpuOverall is not None: cpuoverall_new=cpuOverall.group(2)
except:
searchbox_result = None
Each field is extracted from the following corresponding line:
ramUsed => RAM Utilization 2%
cpuUsed => Total 4%
cpuOverall => Overall CPU load average 12%
ramUsed, cpuUsed, cpuOverall are the variable where I want write the result!!
Corretly line are:
(space undefined) RAM Utilization 2%
(space undefined) Total 4%
(space undefined) Overall CPU load average 12%
When I execute the script all variable return a value: None.
With other variable the script work corretly.
Why the code not work in this case? I use the python3
I think that the problem is a caracter % that not read.
Do you have any suggestions?
PROBLEM 2:
## fp_tmp => pointer of file
for x in fp_tmp:
try:
emailReceived=re.match(r".*(Messages Received)\s+\S+\s+\S+\s+(\S+)",x)
####Not Work####
if emailReceived is not None: emailreceived_new=emailReceived.group(2)
except:
searchbox_result = None
Each field is extracted from the following corresponding on 2 lines in a file:
[....]
Counters: Reset Uptime Lifetime
Receiving
Messages Received 3,406 1,558 3,406
[....]
Rates (Events Per Hour): 1-Minute 5-Minutes 15-Minutes
Receiving
Messages Received 0 0 0
Recipients Received 0 0 0
[....]
I want extract only second occured, that:
Rates (Events Per Hour): 1-Minute 5-Minutes 15-Minutes
Receiving
Messages Received 0 0 0 <-this
Do you have any suggestions?
cpuOverall line: you forgot that there is more information at the start of the line. Change to
'.*(Overall CPU load average)\s+(\S+%)'
cpuUsed line: you forgot that there is more information at the start of the line. Change to
'.*(Total)\s+(\d+%)'
ramUsed line: you forgot that there is more information at the start of the line... Change to
'.*(RAM Utilization)\s+(\d+%)'
Remember that re.match looks for an exact match from the start:
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. [..]
With these changes, your three variables are set to the percentages:
>>> print (cpuused_new,ramused_new,cpuoverall_new)
4% 2% 12%

C++ TensorRT batch inference gives weird results

Good day everyone!
I have a problem performing batch inference in TensorRT. When the batch size is 1 it works like a charm, but when I change it to any other number it gives out plain garbage.
Step by step, I downloaded TensorRT (5.0) and installed it on my Ubuntu 18.04 laptop with GTX755M. I then built the samples that went with it and tested it on sampleMNIST sample and it worked like a charm. I then proceeded to change every occurrence of mParams.batchSize to 10. Of course I also changed the size of allocated memory and modified result printing along. But after I recompiled the sample I got completely weird results - the output says 80% 7 20% 1 for every given input:
grim#shigoto:~/tensorrt/bin$ ./sample_mnist
Building and running a GPU inference engine for MNIST
Input:
############################
############################
############################
############################
############################
################.*##########
################.=##########
############+###.=##########
###########% ###.=##########
###########% ###.=##########
###########+ *##:-##########
###########= *##= ##########
###########. ###= ##########
##########= =++.-##########
########## =##########
########## :*## =##########
##########:*###% =##########
###############% =##########
################ =##########
################ =##########
###############* *##########
###############= ###########
###############= ###########
###############=.###########
###############++###########
############################
############################
############################
Output:
0:
1: ********
2:
3:
4:
5:
6:
7: **
8:
9:
This output repeats 10 times. I've tried this with different networks but results were similar, most of networks give 1 correct output and plain garbage the other 9 times. The complete sample can be found here. I've tried googling documentation but I can't understand what I'm doing wrong. Could you please tell me what am I doing wrong or how to perform batch inference in TensorRT?
Did you also modified the mnist.prototxt?
Especially this part:
input: "data"
input_shape {
dim: 1
dim: 1
dim: 28
dim: 28
}
I think that should be:
input: "data"
input_shape {
dim: 10
dim: 1
dim: 28
dim: 28
}

Perl regexp: how to start matching text in part of file after I have find a "flag" string in the context?

Apologize for the way I describe my question, maybe it will be much clarified if I give the instance as below:
Consider the case that I have a certain file, and it is separated into different section with each beginning with a certain string consider as flag: e.g.
consider "From Clock" as the flag I mentioned about
example_file:
From Clock: fdbk_bufg_cell_in_net
To Clock: fdbk_bufg_cell_in_net
Setup : NA Failing Endpoints, Worst Slack NA , Total Violation NA
Hold : NA Failing Endpoints, Worst Slack NA , Total Violation NA
PW : 0 Failing Endpoints, Worst Slack 5.501ns, Total Violation 0.000ns
Pulse Width Checks
Clock Name: fdbk_bufg_cell_in_net
Waveform(ns): { 0.000 3.500 }
Period(ns): 7.000
Sources: { my_atspeed_mmcm/CLKFBOUT }
Check Type Corner Lib Pin Reference Pin Required(ns) Actual(ns) Slack(ns) Location Pin
Min Period n/a BUFGCE/I n/a 1.499 7.000 5.501 BUFGCE_X0Y35 fdbk_bufg_cell/I
From Clock: mmcm_clkout
To Clock: mmcm_clkout
Setup : NA Failing Endpoints, Worst Slack NA , Total Violation NA
Hold : 0 Failing Endpoints, Worst Slack 0.123ns, Total Violation 0.000ns
PW : 625 Failing Endpoints, Worst Slack -0.195ns, Total Violation -121.875ns
Now I only want to match the "PW :" line in "From Clock: mmcm_clkout" part, how do I do so?
You can try something along these lines:
my $match = 0;
while (<>)
{
if (/^From Clock: (\w+)/)
{
$match = ($1 eq "mmcm_clkout");
} elsif ($match && /^PW: whatever/)
{
# do whatever you want with the line
}
}

calculating program's runtime for each iteration using Unix program time

I wish to calculate runtime of my program which approximates PI using Monte Carlo Method. I have written a unix shell script which feeds the program No of points, N = 10^{k} , for K = 1,2, ...7.
Now I wish to find the execution time for Each N in Unix program time. My unix shell script looks as follows:
#!/bin/bash
k=1
N=10
while [[ k -le 7 ]]
do
echo "$k N" | ./pi $N
((k = k + 1))
((N = N * 10))
done
Now when I type: $ time ./pi.sh , it returns me the total execution time
10 3.6 0.1459155902616465
100 3.08 0.01960555055392467
1000 3.104 0.01196611328551369
10000 3.1284 0.004199352062629202
100000 3.1432 0.0005116342528909465
1000000 3.139704 0.0006011771092076384
10000000 3.141432 5.113762588206346e-05
real 0m0.583s
user 0m0.560s
sys 0m0.012s
I was wondering if I have to write a bash script to get execution time for Each N input or I have to do something in my code.
By writing $ time ./pi.sh you are measuring time needed to execute pi.sh, which has a loop in it. So you measure total time for all iterations. If you want to measure every iteration independently, you have to put time in invocation of the iteration:
echo "$k N" | time ./pi $N

Perl parsing through a file based on conditions

I have a very large log file which is updated periodically. It is as follows:
commands: (List of files to be copied)
Exit time: Fri May 10 05:33:00 2013
Exit status: 2
commands: (List of files to be copied)
Exit Time: Fri May 20 05:34:00 2013
Exit status: 2
commands: (List of files to be copied)
Exit Time: Fri May 30 05:50:00 2013
Exit Status: 1
I have following code which creates a hash based on Exit Status
while ($line = <FH>) {
if ($line =~ /Exit time/) {
($exittime, $exittimeval) = split(': ',$line);
$stat{$qbsid} = {
time => $exittimeval
};
}
I now need to create a timestamp based on localtime such that the script does not compare the log file for the time after the timestamp (localtime). I have the code to compare the time as follows
$date1 = "$hr1:$min1:$sec1, $moy1/$dt1/$yr1";
$date2 = "$hr2:$min2:$sec2, $moy2/$dt2/$yr2";
sub to_comparable {
my ($date) = #_;
my ($H,$M,$S,$d,$m,$Y) = $date =~ m{^(\d+):(\d+):(\d+), (\d+)/(\d+)/(\d+)\z}
or die;
return "$Y$m$d$H$M$S";
}
if (to_comparable($date2) > to_comparable($date1)) {
print "right\n";
} else {
print "wrong \n";
}
Here $hr1,$min1,$sec1,$moy1,$dt1 and $yr1 are local time variables while $hr2,$min2,$sec2, $moy2,$dt2 and $yr2 are values obtained from hash.
Preferably while running for the first time it should compare the whole file and a timestamp is created. Afterwards, the above idea starts.
Please correct me if anything is wrong. Thank you.
You might want to consider using Time::Piece, which was first released with perl v5.9.5.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
{
my $end_date = '2013-05-30';
local $/ = '';
while (<DATA>) {
if (/^Exit Time: (.+)/m) {
my $date = Time::Piece->strptime($1, "%c");
print $date->ymd, "\n" if $date->ymd lt $end_date;
}
}
}
__DATA__
commands: (List of files to be copied)
Exit Time: Fri May 10 05:33:00 2013
Exit status: 2
commands: (List of files to be copied)
Exit Time: Fri May 20 05:34:00 2013
Exit status: 2
commands: (List of files to be copied)
Exit Time: Fri May 30 05:50:00 2013
Exit Status: 1
Output:
2013-05-10
2013-05-20
You'll be forming a 20-digit number (assuming years are 4-digit and the rest are always exactly 2-digit). That's a big number, but it seems fine on my 64-bit UNIX OS; I don't know about yours. Anyway, with a fixed-length string, you could do a string comparison ("ge" instead of ">") if a number that big is an issue.
If any of the inputs (e.g. $moy1) could be a single digit, then your comparison function will not work since the October 1st (2013101) would be before September 30th (2013930). You could require a fixed number of digits using:
my ($H,$M,$S,$d,$m,$Y) = $date =~ m{^(\d\d):(\d\d):(\d\d), (\d\d)/(\d\d)/(\d\d\d\d)\z}
or die;
I'm sure how $qbsid is set (from Exit Status or something else), but since your code isn't complete I assume you have something else to do that.
I'm also not sure how your original time strings (e.g. "Fri May 30 05:50:00 2013") get transformed into the "$hr1:$min1:$sec1, $moy1/$dt1/$yr1" format, but I assume you do that elsewhere as well.