logo

Crowdly

Browser

Add to Chrome

Haystack Unit tests can be tedious to write by hand. Instead, we can sometime...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

Haystack

Unit tests can be tedious to write by hand. Instead, we can sometimes achieve good results by testing with random values. This is not always possible, because we need an oracle to tell us if our test results are correct. But in this assignment, we shall look at a case where the oracle is easy to write. / Eenheidstoetse kan vervelig wees as dit met die hand geskryf is. In plaas daarvan kan ons soms goeie resultate behaal deur met ewekansige waardes te toets. Dit is nie altyd moontlik nie, want ons het 'n orakel nodig om vir ons te sê of ons toetsuitslae korrek is. Maar in hierdie opdrag gaan ons kyk na 'n geval waar die orakel maklik is om te skryf.

Download the BadSearch.py module that implements two search functions. Now create the program called testsearch.py to test the BadSearch.find0() method with random parameters. Start by writing a method def randomTest(size) that implements the following steps: / Laai die BadSearch.py program af wat twee soek funksies implementeer. Skep nou 'n program testsearch.py om die BadSearch.find0() metode met ewekansige parameters te toets. Begin deur 'n metode def randomTest(size) te skryf wat die volgende stappe implementeer:

  1. Pick a random target value k from 0 to 49. / Kies 'n ewekansige teikenwaarde k van 0 tot 49.

  2. Construct a new integer array of size size. / Konstrueer 'n nuwe heelgetal-skikking van grootte size.

  3. Fill the array with random values from 0 to 49. / Vul die skikking met ewekansige waardes van 0 tot 49.

  4. If a random value you are placing in the array is equal to k, make a note of the index where the value is placed. / As 'n ewekansige waarde wat jy in die skikking plaas gelyk is aan k, maak 'n nota van die indeks waar die waarde geplaas is.

  5. If none of the random values are equal to k, the noted index should be -1. / As nie een van die ewekansige waardes gelyk is aan k nie, moet die genoteerde indeks -1 wees.

  6. Call BadSearch.find0() and return true if and only if it returns the noted index. / Bel BadSearch0.find() en gee true as en slegs as dit die genoteerde indeks terugstuur.

In the main function, call randomTest(size) for different array sizes: 100,000 times for size 2, another 100,000 times for size 3, and so on, up to 100,000 times for size 7. For each size, record how many times randomTest() detected an error (= returned False), and display this statistic. / In die main funksie, roep randomTest(size) vir verskillende skikkingsgroottes: 100 000 keer vir grootte 2, nog 100 000 keer vir grootte 3, ensovoorts, tot 100 000 keer vir grootte 7. Vir elke grootte, teken aan hoeveel keer randomTest( ) 'n fout opgespoor het (= returned False), en vertoon hierdie statistiek.

$ python testsearch.py

n:2 bug: 96.1%

n:3 bug: 94.2%

n:4 bug: 92.4%

n:5 bug: 90.5%

n:6 bug: 88.5%

n:7 bug: 86.9%

Repeat all of the above for BadSearch.find1(). / Herhaal al die bogenoemde vir BadSearch.find1().

More questions like this

Want instant access to all verified answers on stemlearn.sun.ac.za?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome