Sieve of Eratosthenes - Tests





2
Date Submitted Sun. Apr. 29th, 2007 10:14 PM
Revision 1 of 1
Helper esumerfd
Tags "Sieve | Eratosthenes" | groovy | of | primes
Comments 0 comments
The tests for the Sieve of Eratosthenes snippets.
package simulation.sieve;

import simulation.sieve.try1.*
import simulation.sieve.try2.*
import simulation.sieve.try3.*
import simulation.sieve.try4.*
import simulation.sieve.try5.*

class SieveTest extends GroovyTestCase
{
    def answers = [
        2, 3, 5, 7,11,13,17,19,23,29,
       31,37,41,43,47,53,59,61,67,71,
       73,79,83,89,97
    ]

    void testSeive1()
    {
        check(Sieve1.newInstance())
    }

    void testSeive2()
    {
        check(Sieve2.newInstance())
    }

    void testSeive3()
    {
        check(Sieve3.newInstance())
    }

    void testSeive4()
    {
        check(Sieve4.newInstance())
    }

    void testSeive5()
    {
        check(Sieve5.newInstance())
    }
   
    def check(a_sieve)
    {
        def primes

        def duration = averageTime(5)
        {
            primes = a_sieve.eratosthenes(100)
        }
       
        println "Time: ${duration}ms: Algorithm: ${a_sieve.class.simpleName} Primes: ${primes}"
       
        assert answers == primes
    }

    def averageTime(a_count, a_closure)
    {
        def durations = []

        a_count.times {
           
            durations << time(a_closure)
        }
       
        def total = 0
        durations.each { duration ->
           
            total += duration
        }
       
        total / durations.size()
    }

    def time(a_closure)
    {
        def start = System.currentTimeMillis()
        a_closure()
        def end = System.currentTimeMillis()
       
        end - start
    }
}


...

 

Edward Sumerfield

Comments

There are currently no comments for this snippet.

Voting

Votes Down


Helper morad