Stap 4: Voorbeelden en Test cases
Laten we oefenen de module, met sommige testgevallen.
Gebruik van de module we moeten
- de locatie van de module toevoegen aan de padlijst
- Dit gebeurt na op de eerste regel met - I. /
- . / wordt ervan uitgegaan dat de module is in dezelfde map als het script
- Voeg de regel "gebruik paarsgewijze;"
- Opmerking de naam van het pakket, de bestandsnaam van de module werden gekozen op basis van
De teststubs_pairwise.pl bevat een lopende versie van deze voorbeelden:
#!/usr/bin/perl -I ./ <br>use strict; use Data::Dumper; use pairwise; ################## teststub1(); teststub2(); teststub3(); teststub4(); ################## #teststubs listed here
Test-case 1: wie is met mij.
Donuts zijn een serieuze zaak als u wilt dat uw billijk aandeel.
Niets zegt dat wij donuts serieus als een oproep tot actie te automatiseren.
In het eerste voorbeeld zullen we bouwen een contact vereniging kaart & en maken van een oproep tot actie.
Code
sub teststub1 {<br> print "teststub1\n"; my $kvp1 = pairwise->new(); $kvp1->addkvp('Chad' ,'locust team'); $kvp1->addkvp('Mike' ,'locust team'); $kvp1->addkvp('Dave' ,'locust team'); $kvp1->addkvp('Jon' ,'locust team'); $kvp1->addkvp('Chad' ,'volunteer'); $kvp1->addkvp('Jon' ,'volunteer'); $kvp1->addkvp('Steve' ,'Citizen Forester'); $kvp1->addkvp('Dave' ,'Music Fan'); # print Dumper $kvp1; print $kvp1->map2str() . "\n"; $kvp1->invertkvp(); print $kvp1->map2str() . "\n"; my $locust_team = $kvp1->getvalue('locust team'); print "locust team Assemble! Attn:" . join (' ', . "\n"; return; }
Uitvoer (die donuts niet een kans!)
teststub1 Dave,1,Music Fan Dave,1,locust team Jon,1,locust team Jon,1,volunteer Mike,1,locust team Chad,1,locust team Chad,1,volunteer Steve,1,Citizen Forester Music Fan,1,Dave locust team,1,Dave locust team,1,Mike locust team,1,Jon locust team,1,Chad Citizen Forester,1,Steve volunteer,1,Jon volunteer,1,Chad locust team Assemble! Attn:Dave Mike Jon Chad
Test-Case 2: 1 aardappel 2 aardappel...
Vinden we uit als er meer dan 1 aardappel is
Code
sub teststub2 {<br> print "teststub2\n"; my $kvp1 = pairwise->new(); $kvp1->addkvp('potato1' ,'red'); $kvp1->addkvp('potato2' ,'blue'); $kvp1->addkvp('potato1' ,'blue'); $kvp1->addkvp('potato1' ,'red'); $kvp1->addkvp('potato2' ,'blue'); $kvp1->addkvp('potato3' ,'red'); $kvp1->addkvp('potato4' ,'red'); $kvp1->addkvp('potato4' ,'red'); #print $kvp1->map2str() . "\n"; $kvp1->invertkvp(); #print $kvp1->map2str() . "\n"; #Mr Potato impersonators... my $redpotatos = $kvp1->getvalue('red'); if (scalar( gt '1') { print "More then 1 red potato here \n"; } else { print "Nothing is here ... move along \n"; } }
Output (Aankondiging van de grootte van de matrix geeft een indicatie van de sterkte van de koppeling)
teststub2 More than 1 red potato here
Test-Case 3: De jacht voor de rode aardappel
Er is een concept van in de logica. Modus Tollens. MT neer "ontkennen het daaruit voortvloeiende."
Anders gezegd, als niet q hebt dan heb je niet p en p q impliceert
Wij zullen dit gebruiken als onderdeel van een proces van eliminatie om het vinden van de ware---uhhh---rode aardappel.
Code
sub teststub3 { print "teststub3\n"; my $kvp1 = pairwise->new(); $kvp1->addkvp('potato1' ,'blue'); $kvp1->addkvp('potato2' ,'red'); $kvp1->addkvp('potato1' ,'red'); $kvp1->addkvp('potato1' ,'blue'); $kvp1->addkvp('potato2' ,'red'); $kvp1->addkvp('potato3' ,'blue'); $kvp1->addkvp('potato4' ,'blue'); $kvp1->addkvp('potato4' ,'blue'); print "starting with : \n"; print $kvp1->map2str() . "\n"; $kvp1->invertkvp(); my $bluelist = $kvp1->getvalue('blue'); foreach my $tot ( $kvp1->removekey($tot); print "removing $tot from consideration\n"; } print "Modus Tolens NOT blue : \n"; print $kvp1->map2str() . "\n"; }
Output (Merken ondanks meerdere associatieve koppelingen, de aardappels met blauwe verenigingen werden in 1 stap verwijderd)
More then 1 red potato here <br>teststub3 starting with : potato4,2,blue potato2,2,red potato3,1,blue potato1,2,blue potato1,1,red removing potato4 from consideration removing potato3 from consideration removing potato1 from consideration Modus Tolens NOT blue : potato2,2,red
Test-Case 4: Histogrammen & vinden van kruispunten
Dus als waarden een coördinaat voor een specifieke sleutel vertegenwoordigen, kan het mogelijk om te bepalen van kruispunten
Code:
sub teststub4 {<br> print "teststub4 finding interesection of defined points\n"; my $kvp1 = pairwise->new(); $kvp1->addkvp('wire1' ,'2,1'); $kvp1->addkvp('wire1' ,'2,2'); $kvp1->addkvp('wire1' ,'2,3'); $kvp1->addkvp('wire1' ,'2,4'); $kvp1->addkvp('wire1' ,'2,4'); $kvp1->addkvp('wire2' ,'4,2'); $kvp1->addkvp('wire2' ,'3,2'); $kvp1->addkvp('wire2' ,'2,2'); $kvp1->addkvp('wire2' ,'1,2'); print "starting with :\n"; print $kvp1->map2str() . "\n"; $kvp1->invertkvp(); my $multi = $kvp1->nonuniqkeys(); print "nonuniqkeys returns a hash of keys with multiple associations / overlaps\n" . Dumper $multi; $multi = $kvp1->nonuniqkvp(); print "nonuniqkeys returns a hash of kvp defined multiple times \n" . Dumper $multi; }
Output: (Merk op dat het veld komma's bevat en standaard het scheidingsteken een komma is. Dit illustreert hoe tokenizing kan worden gemaakt ingewikkelder. De seperator-functie kan worden gebruikt om te veranderen wat de map2str-methode sluit zich aan bij de velden op... Anderzijds kan een strategisch te profiteren van de situatie. In beide gevallen kan consequente herhaalbare uitvoering de ontwikkelaar om te begrijpen en te werken met het gedrag. )
teststub4 finding intersection of defined points<br>starting with : wire1,1,2,3 wire1,1,2,2 wire1,2,2,4 wire1,1,2,1 wire2,1,3,2 wire2,1,1,2 wire2,1,4,2 wire2,1,2,2 nonuniqkeys returns a hash of keys with multiple associations / overlaps $VAR1 = { '2,2' => { 'wire1' => 1, 'wire2' => 1 } }; nonuniqkeys returns a hash of kvp difined multiple times $VAR1 = { '2,4' => { 'wire1' => 2 } };