Commit 18adf57e authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman 💥
Browse files

A javascript example of how to calculate primes with jasmine unit tests.

parents
function isPrime(number) {
if ((number == 2) || (number == 3))
return true;
if ((number % 2 === 0) || (number % 3 === 0))
return false;
var maxDivisor = Math.sqrt(number);
var dividendIndex = 1;
var dividend = 5;
while (dividend < maxDivisor) {
if (number % dividend === 0)
return false;
dividend + 2;
if (dividend > maxDivisor)
true;
if (number % dividend === 0)
return false;
dividendIndex++;
dividend = 6 * dividendIndex - 1;
}
return true;
}
\ No newline at end of file
describe("Primality", function() {
it("should determine 2 is a prime number", function() {
expect(isPrime(2)).toBeTruthy();
});
it("should determine 3 is a prime number", function() {
expect(isPrime(3)).toBeTruthy();
});
it("should determine 4 is not a prime number", function() {
expect(isPrime(4)).toBeFalsy();
});
it("should determine 5 is a prime number", function() {
expect(isPrime(5)).toBeTruthy();
});
it("should determine 6 is not a prime number", function() {
expect(isPrime(6)).toBeFalsy();
});
it("should determine 7 is a prime number", function() {
expect(isPrime(7)).toBeTruthy();
});
it("should determine 8 is not a prime number", function() {
expect(isPrime(8)).toBeFalsy();
});
it("should determine 9 is not a prime number", function() {
expect(isPrime(9)).toBeFalsy();
});
it("should determine 10 is not a prime number", function() {
expect(isPrime(10)).toBeFalsy();
});
it("should determine 11 is a prime number", function() {
expect(isPrime(11)).toBeTruthy();
});
it("should determine 12 is not a prime number", function() {
expect(isPrime(12)).toBeFalsy();
});
it("should determine 13 is a prime number", function() {
expect(isPrime(13)).toBeTruthy();
});
it("should determine 14 is not a prime number", function() {
expect(isPrime(14)).toBeFalsy();
});
it("should determine 15 is not a prime number", function() {
expect(isPrime(15)).toBeFalsy();
});
it("should determine 16 is not a prime number", function() {
expect(isPrime(16)).toBeFalsy();
});
it("should determine 17 is a prime number", function() {
expect(isPrime(17)).toBeTruthy();
});
it("should determine 18 is not a prime number", function() {
expect(isPrime(18)).toBeFalsy();
});
it("should determine 19 is a prime number", function() {
expect(isPrime(19)).toBeTruthy();
});
it("should determine 20 is not a prime number", function() {
expect(isPrime(20)).toBeFalsy();
});
it("should determine 37,120,123 is a prime number", function() {
expect(isPrime(37120123)).toBeTruthy();
});
});
\ No newline at end of file
Using an algorithmically efficient means to calculate if a number is prime or
not. Also uses a simple Jasmine unit test to demonstrate it.
\ No newline at end of file
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Primality Unit Tests</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-1.3.0/jasmine_favicon.png" />
<!-- include source files here... -->
<link data-require="jasmine@*" data-semver="1.3.1" rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" />
<script data-require="jasmine@*" data-semver="1.3.1" src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script data-require="jasmine@*" data-semver="1.3.1" src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<script data-require="json2@*" data-semver="0.0.2012100-8" src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script>
<!-- include src files here... -->
<script type="text/javascript" src="Primality.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="Primality.spec.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
</script>
</head>
<body></body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment