Skip to content

Commit cc1ded3

Browse files
committed
test-generator/generate-test.pl: A lot of changes to improve generation process and correctness of the generator
Signed-off-by: Marian Marinov <[email protected]>
1 parent 34d6222 commit cc1ded3

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

test-generator/generate-test.pl

+54-31
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use DBD::Pg;
66
use PDF::Reuse;
77
use utf8;
8+
no warnings 'utf8';
9+
use Data::Dumper;
810

911
#
1012
# ./generate-tests.pl [variant]
@@ -16,23 +18,25 @@ sub logger {
1618
print 'Error: ' . $_[0] ."\n" if defined($_[0]);
1719
}
1820

21+
# define which test we are going to generate, first or second
22+
my $first_test = 1;
1923
my $debug = 0;
2024
my $pguser = 'smalusr';
2125
my $pgpass = 'kokoshka';
2226
my $pgdb = 'DBI:Pg:database=smal;host=localhost;port=5432';
2327
my $pgconn = DBI->connect_cached( $pgdb, $pguser, $pgpass, { PrintError => 1, AutoCommit => 1 }) or die("$DBI::errstr\n");
2428
my $schema = 'public';
25-
# define which test we are going to generate, first or second
26-
my $first_test = 0;
2729
my %ques = ();
2830
my $qcount = 0;
2931
my $question_count = 1;
3032
my $max_questions = 50;
3133
my $variant = 1;
3234
$variant = $ARGV[0] if (defined($ARGV[0]) && $ARGV[0] =~ /^[0-9]+$/);
33-
my $filename = "test1-variant$variant";
35+
my $filename = $first_test ? "test1-variant$variant" : "test2-variant$variant";
3436
my $first_qid;
3537
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
38+
# define which test we are going to generate, first or second
39+
my $first_test = 1;
3640

3741
$year = $year+1900;
3842

@@ -57,9 +61,16 @@ sub logger {
5761
}
5862
my $fqid = $first_qid->execute();
5963
print "First quid: $fqid\n" if $debug;
60-
61-
#my $get_question = $pgconn->prepare(sprintf('SELECT question FROM "%s".questions WHERE id = ? OFFSET random()*%d LIMIT 1', $schema, $qcount))
62-
my $get_question = $pgconn->prepare(sprintf('SELECT question FROM "%s".questions WHERE id = ? LIMIT 1', $schema))
64+
my %all_questions = ();
65+
my $max_q = 0;
66+
my $q_ids = $pgconn->prepare(sprintf('SELECT id,first_test FROM "%s".questions ORDER BY id', $schema))
67+
or logger($DBI::errstr);
68+
$q_ids->execute();
69+
foreach my $q_id(@{$q_ids->fetchall_arrayref}) {
70+
$all_questions{$$q_id[0]} = $$q_id[1];
71+
$max_q = $$q_id[0];
72+
}
73+
my $get_question = $pgconn->prepare(sprintf('SELECT question FROM "%s".questions WHERE id = ? AND %s first_test LIMIT 1', $schema, $first_test ? '' : 'NOT' ))
6374
or logger($DBI::errstr);
6475
my $right_asnwares = $pgconn->prepare(sprintf('SELECT answer FROM "%s".right_answers WHERE q_id = ?', $schema))
6576
or logger($DBI::errstr);
@@ -80,17 +91,27 @@ sub logger {
8091
my $id = $id_compensation + int(rand($qcount));
8192
# generate random question positions
8293
while ($question_count <= $max_questions) {
83-
print "Num: $question_count" if $debug;
94+
printf "Num: %2d", $question_count if $debug;
8495
my $rcount = 0;
85-
until (! exists $questions_check{$id} && ! exists $excluded{$id}) {
86-
$id = $id_compensation + int(rand($qcount));
87-
if ($rcount == 350) {
88-
print "Too random\n" if $debug;
96+
while (1) {
97+
$id = $id_compensation + int(rand($max_q));
98+
next if (exists $questions_check{$id});
99+
if ($first_test and $all_questions{$id}) {
100+
$questions_check{$id} = 1;
89101
last;
90102
}
103+
if (!$first_test and !$all_questions{$id}) {
104+
$questions_check{$id} = 1;
105+
last;
106+
}
107+
if ($rcount > 350) {
108+
# We can't find a random question id, that is also part of the QIDs from the DB.
109+
print " - Too random - " if $debug;
110+
next;
111+
}
91112
$rcount++;
92113
}
93-
print " q_id: $id\n" if $debug;
114+
printf " q_id: %3d %d\n", $id, $all_questions{$id} if $debug;
94115
$questions_check{$id} = 1;
95116
$ques{$question_count} = [ -1, -1, -1, -1, -1, -1 ];
96117
$ques{$question_count}[6] = $id;
@@ -99,25 +120,30 @@ sub logger {
99120
$ques{$question_count}[0] = $location;
100121
$question_count++;
101122
}
102-
for (sort keys(%ques)) {print "$_ not defined\n" if (!defined($ques{$_}[0]));}
123+
124+
# Check if there are questions without correct answer location
125+
if ($debug) {
126+
for (sort keys(%ques)) {print "$_ not defined\n" if (!defined($ques{$_}[0]));}
127+
}
103128

104129
my $page_count = 1;
105130
# Starting position on the page(after the answer boxes)
106-
my $last_line = 666;
131+
my $last_line = 700;
107132

108133
sub page_check {
109134
my $last_pos = $_[0];
110135
my $page_count = $_[1];
111-
print 'Page: '.${$page_count}.' Line: '.${$last_pos}."\n" if $debug;
136+
# print 'Page: '.${$page_count}.' Line: '.${$last_pos}."\n" if $debug;
112137
${$last_pos} -= 16;
113138
if (${$last_pos} < 40) {
114139
if (${$page_count} == 1) {
115140
prAdd("0.0 0.0 0.0 RG\n");
116141
prAdd("9.0 9.0 9.0 rg\n");
142+
# Generate the answer boxes
117143
for my $l (1..25) {
118144
my $pos = ($l * 18) + 20;
119-
prAdd("$pos 711 18 20 re\n");
120-
prAdd("$pos 676 18 20 re\n");
145+
prAdd("$pos 744 18 20 re\n");
146+
prAdd("$pos 712 18 20 re\n");
121147
}
122148
prAdd("B\n");
123149
}
@@ -132,18 +158,18 @@ sub page_check {
132158
prFile("$filename.pdf");
133159
prTTFont('/usr/share/fonts/arial.ttf');
134160
prText(35,800,"Linux System & Network Administration");
135-
prText(340,800,"TEST $test $year");
161+
prText(340,800,"TEST $test $year variant: $variant");
136162
prText(532,800,"Page $page_count");
137-
prText(35,780,"Name: ______________________________________________________________________");
138-
prText(35,760,"SoftUni username: ______________________________");
139-
prText(510,760," Variant: $variant");
163+
prText(35,780,"Name: _______________________________________________________ FN:_______________");
164+
165+
# Generate the answer numbers
140166
for my $l (1..25) {
141167
my $pos = ($l * 18) + 24;
142168
if ($l > 9) {
143169
$pos = ($l * 18) + 22;
144170
}
145-
prText($pos,734,$l);
146-
prText($pos-1,699,$l+25);
171+
prText($pos,766,$l);
172+
prText($pos-1,734,$l+25);
147173
}
148174

149175
prFontSize('10');
@@ -218,25 +244,24 @@ sub page_check {
218244
if ($q_len > 92) {
219245
my @lines = split /\n/, $ques{$qid}[5];
220246
for (my $l=0;$l<=$#lines;$l++) {
221-
$lines[$l] =~ s/\n/ /g;
222247
if ($l==0) {
223-
prText(35,$last_line,sprintf('%d. %s', $qid, $lines[$l]));
248+
prText(35,$last_line,sprintf('%d. %ls', $qid, $lines[$l]));
224249
} else {
225250
prText(35,$last_line,$lines[$l]) if ($lines[$l] !~ /^[\s|\n]*$/);
226251
}
227252
page_check(\$last_line,\$page_count);
228253
}
229254
} else {
230-
prText(35,$last_line,sprintf('%d. %s', $qid, $ques{$qid}[5]));
255+
prText(35,$last_line,sprintf('%d. %ls', $qid, $ques{$qid}[5]));
231256
}
232257

233258
my %a_names = ( 1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd' );
234259
$entry_count = 1;
235260
for (my $c=1;$c<5;$c++) {
236261
if (defined($ques{$qid}[$c]) && $ques{$qid}[$c] ne '-1') {
237-
printf(" %s) %s\n", $a_names{$entry_count}, $ques{$qid}[$c]) if $debug;
262+
printf(" %ls) %ls\n", $a_names{$entry_count}, $ques{$qid}[$c]) if $debug;
238263
page_check(\$last_line,\$page_count);
239-
prText(35,$last_line,sprintf(" %s) %s", $a_names{$entry_count}, $ques{$qid}[$c]));
264+
prText(35,$last_line,sprintf(" %ls) %ls", $a_names{$entry_count}, $ques{$qid}[$c]));
240265
$entry_count++;
241266
}
242267
}
@@ -256,15 +281,13 @@ sub page_check {
256281
# print the right answares :)
257282
$tcount = 1;
258283
print "\nNum: \n" if $debug;
259-
# prAdd("0.0 0.0 0.0 RG\n");
260-
#for (sort keys(%ques)) {
261284
for (1..$max_questions) {
262285
print "$_ not defined\n" if (!defined($ques{$_}[0]) && $debug);
263286
my $right = 'a';
264287
$right = 'b' if ($ques{$_}[0] == 2);
265288
$right = 'c' if ($ques{$_}[0] == 3);
266289
$right = 'd' if ($ques{$_}[0] == 4);
267-
printf("%2d: %s \n",$tcount, $right) if $debug;
290+
printf("%2d: %ls \n",$tcount, $right) if $debug;
268291
prAdd("q 38 717 m 490 717 l S Q");
269292
prAdd("q 38 698 m 490 698 l S Q");
270293
prAdd("q 38 677 m 490 677 l S Q");

0 commit comments

Comments
 (0)