mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
chore: merge Fix/742 migrate doctest to jest (#749)
* Remove QuickSelect doctest There are more Jest test cases already. * Remove AverageMedian doctest Already migrated to jest * Migrate doctest for BinaryExponentiationRecursive.js (also remove inline "main" test method) * Migrate doctest for EulersTotient.js (also remove inline "main" test method) * Migrate doctest for PrimeFactors.js (also remove inline "main" test method) * Migrate doctest for BogoSort.js Re-write prototype-polluting helper methods, too. (also remove inline test driver code) * Migrate doctest for BeadSort.js (also remove inline test driver code) * Migrate doctest for BucketSort.js (also remove inline test driver code) * Migrate doctest for CocktailShakerSort.js (also remove inline test driver code) * Migrate doctest for MergeSort.js (also remove inline test driver code) * Migrate doctest for QuickSort.js (also remove inline test driver code) * Migrate doctest for ReverseString.js (also remove inline test driver code) * Migrate doctest for ReverseString.js * Migrate doctest for ValidateEmail.js * Migrate doctest for ConwaysGameOfLife.js (remove the animate code, too) * Remove TernarySearch doctest Already migrated to jest * Migrate doctest for BubbleSort.js (also remove inline test driver code) * Remove doctest from CI and from dependencies relates to #742 fixes #586 * Migrate doctest for RgbHsvConversion.js * Add --fix option to "standard" npm script * Migrate doctest for BreadthFirstSearch.js (also remove inline test driver code) * Migrate doctest for BreadthFirstShortestPath.js (also remove inline test driver code) * Migrate doctest for EulerMethod.js (also remove inline test driver code) Move manual test-code for plotting stuff in the browser in a distinct file, too. Those "*.manual-test.js" files are excluded from the UpdateDirectory.mjs script, as well. * Migrate doctest for Mandelbrot.js (also remove inline test driver code & moved manual drawing test into a *.manual-test.js) * Migrate doctest for FloodFill.js * Migrate doctest for KochSnowflake.js (also move manual drawing test into a *.manual-test.js) * Update npm lockfile * Update README and COMMITTING with a few bits & bobs regarding testing & code quality
This commit is contained in:
@@ -8,19 +8,9 @@
|
||||
* else if the length of the array is odd number, the median value will be the middle number in the array
|
||||
*/
|
||||
|
||||
/*
|
||||
* Doctests
|
||||
*
|
||||
* > averageMedian([8, 9, 1, 2, 5, 10, 11])
|
||||
* 8
|
||||
* > averageMedian([15, 18, 3, 9, 13, 5])
|
||||
* 11
|
||||
* > averageMedian([1,2,3,4,6,8])
|
||||
* 3.5
|
||||
*/
|
||||
|
||||
const averageMedian = (numbers) => {
|
||||
let median = 0; const numLength = numbers.length
|
||||
let median = 0
|
||||
const numLength = numbers.length
|
||||
numbers = numbers.sort(sortNumbers)
|
||||
|
||||
if (numLength % 2 === 0) {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
https://en.wikipedia.org/wiki/Exponentiation_by_squaring
|
||||
*/
|
||||
|
||||
const binaryExponentiation = (a, n) => {
|
||||
export const binaryExponentiation = (a, n) => {
|
||||
// input: a: int, n: int
|
||||
// returns: a^n: int
|
||||
if (n === 0) {
|
||||
@@ -18,14 +18,3 @@ const binaryExponentiation = (a, n) => {
|
||||
return b * b
|
||||
}
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
// binary_exponentiation(2, 10)
|
||||
// > 1024
|
||||
console.log(binaryExponentiation(2, 10))
|
||||
// binary_exponentiation(3, 9)
|
||||
// > 19683
|
||||
console.log(binaryExponentiation(3, 9))
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
@@ -1,28 +1,19 @@
|
||||
/*
|
||||
In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most basic explicit method for numerical integration of ordinary differential equations. The method proceeds in a series of steps. At each step the y-value is calculated by evaluating the differential equation at the previous step, multiplying the result with the step-size and adding it to the last y-value: y_n+1 = y_n + stepSize * f(x_n, y_n).
|
||||
(description adapted from https://en.wikipedia.org/wiki/Euler_method )
|
||||
(see also: https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ )
|
||||
*/
|
||||
|
||||
/*
|
||||
Doctests
|
||||
> eulerStep(0, 0.1, 0, function(x, y){return x})
|
||||
0
|
||||
> eulerStep(2, 1, 1, function(x, y){return x * x})
|
||||
5
|
||||
> eulerFull(0, 3, 1, 0, function(x, y){return x})
|
||||
[{"x": 0, "y": 0}, {"x": 1, "y": 0}, {"x": 2, "y": 1}, {"x": 3, "y": 3}]
|
||||
> eulerFull(3, 4, 0.5, 1, function(x, y){return x * x})
|
||||
[{"x": 3, "y": 1}, {"x": 3.5, "y": 5.5}, {"x": 4, "y": 11.625}]
|
||||
*/
|
||||
|
||||
function eulerStep (xCurrent, stepSize, yCurrent, differentialEquation) {
|
||||
/**
|
||||
* In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order
|
||||
* numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most
|
||||
* basic explicit method for numerical integration of ordinary differential equations. The method proceeds in a series
|
||||
* of steps. At each step the y-value is calculated by evaluating the differential equation at the previous step,
|
||||
* multiplying the result with the step-size and adding it to the last y-value: y_n+1 = y_n + stepSize * f(x_n, y_n).
|
||||
*
|
||||
* (description adapted from https://en.wikipedia.org/wiki/Euler_method)
|
||||
* @see https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
||||
*/
|
||||
export function eulerStep (xCurrent, stepSize, yCurrent, differentialEquation) {
|
||||
// calculates the next y-value based on the current value of x, y and the stepSize
|
||||
const yNext = yCurrent + stepSize * differentialEquation(xCurrent, yCurrent)
|
||||
return yNext
|
||||
return yCurrent + stepSize * differentialEquation(xCurrent, yCurrent)
|
||||
}
|
||||
|
||||
function eulerFull (xStart, xEnd, stepSize, yStart, differentialEquation) {
|
||||
export function eulerFull (xStart, xEnd, stepSize, yStart, differentialEquation) {
|
||||
// loops through all the steps until xEnd is reached, adds a point for each step and then returns all the points
|
||||
const points = [{ x: xStart, y: yStart }]
|
||||
let yCurrent = yStart
|
||||
@@ -37,72 +28,3 @@ function eulerFull (xStart, xEnd, stepSize, yStart, differentialEquation) {
|
||||
|
||||
return points
|
||||
}
|
||||
|
||||
function plotLine (label, points, width, height) {
|
||||
// utility function to plot the results
|
||||
|
||||
// container needed to control the size of the canvas
|
||||
const container = document.createElement('div')
|
||||
container.style.width = width + 'px'
|
||||
container.style.height = height + 'px'
|
||||
document.body.append(container)
|
||||
|
||||
// the canvas for plotting
|
||||
const canvas = document.createElement('canvas')
|
||||
container.append(canvas)
|
||||
|
||||
// Chart-class from chartjs
|
||||
const chart = new Chart(canvas, { // eslint-disable-line
|
||||
type: 'scatter',
|
||||
data: {
|
||||
datasets: [{
|
||||
label: label,
|
||||
data: points,
|
||||
showLine: true,
|
||||
fill: false,
|
||||
tension: 0,
|
||||
borderColor: 'black'
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
maintainAspectRatio: false,
|
||||
responsive: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function exampleEquation1 (x, y) {
|
||||
return x
|
||||
}
|
||||
|
||||
// example from https://en.wikipedia.org/wiki/Euler_method
|
||||
function exampleEquation2 (x, y) {
|
||||
return y
|
||||
}
|
||||
|
||||
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
||||
function exampleEquation3 (x, y) {
|
||||
return x + y + x * y
|
||||
}
|
||||
|
||||
const points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1)
|
||||
const points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2)
|
||||
const points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3)
|
||||
|
||||
console.log(points1)
|
||||
console.log(points2)
|
||||
console.log(points3)
|
||||
|
||||
// plot the results if the script is executed in a browser with a window-object
|
||||
if (typeof window !== 'undefined') {
|
||||
const script = document.createElement('script')
|
||||
|
||||
// using chartjs
|
||||
script.src = 'https://www.chartjs.org/dist/2.9.4/Chart.min.js'
|
||||
script.onload = function () {
|
||||
plotLine('example 1: dy/dx = x', points1, 600, 400)
|
||||
plotLine('example 2: dy/dx = y', points2, 600, 400)
|
||||
plotLine('example 3: dy/dx = x + y + x * y', points3, 600, 400)
|
||||
}
|
||||
document.body.append(script)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
O(sqrt(n))
|
||||
*/
|
||||
|
||||
const EulersTotient = (n) => {
|
||||
export const EulersTotient = (n) => {
|
||||
// input: n: int
|
||||
// output: phi(n): count of numbers b/w 1 and n that are coprime to n
|
||||
let res = n
|
||||
@@ -27,14 +27,3 @@ const EulersTotient = (n) => {
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
// EulersTotient(9) = 6 as 1, 2, 4, 5, 7, and 8 are coprime to 9
|
||||
// > 6
|
||||
console.log(EulersTotient(9))
|
||||
// EulersTotient(10) = 4 as 1, 3, 7, 9 are coprime to 10
|
||||
// > 4
|
||||
console.log(EulersTotient(10))
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
@@ -1,43 +1,22 @@
|
||||
/**
|
||||
* The Mandelbrot set is the set of complex numbers "c" for which the series "z_(n+1) = z_n * z_n +
|
||||
* c" does not diverge, i.e. remains bounded. Thus, a complex number "c" is a member of the
|
||||
* Mandelbrot set if, when starting with "z_0 = 0" and applying the iteration repeatedly, the
|
||||
* absolute value of "z_n" remains bounded for all "n > 0". Complex numbers can be written as "a +
|
||||
* b*i": "a" is the real component, usually drawn on the x-axis, and "b*i" is the imaginary
|
||||
* component, usually drawn on the y-axis. Most visualizations of the Mandelbrot set use a
|
||||
* color-coding to indicate after how many steps in the series the numbers outside the set cross the
|
||||
* divergence threshold. Images of the Mandelbrot set exhibit an elaborate and infinitely
|
||||
* complicated boundary that reveals progressively ever-finer recursive detail at increasing
|
||||
* magnifications, making the boundary of the Mandelbrot set a fractal curve. (description adapted
|
||||
* from https://en.wikipedia.org/wiki/Mandelbrot_set ) (see also
|
||||
* https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set )
|
||||
*/
|
||||
|
||||
/*
|
||||
Doctests
|
||||
Test black and white
|
||||
Pixel outside the Mandelbrot set should be white.
|
||||
Pixel inside the Mandelbrot set should be black.
|
||||
> getRGBData(800, 600, -0.6, 0, 3.2, 50, false)[0][0]
|
||||
[255, 255, 255]
|
||||
> getRGBData(800, 600, -0.6, 0, 3.2, 50, false)[400][300]
|
||||
[0, 0, 0]
|
||||
|
||||
Test color-coding
|
||||
Pixel distant to the Mandelbrot set should be red.
|
||||
Pixel inside the Mandelbrot set should be black.
|
||||
> getRGBData(800, 600, -0.6, 0, 3.2, 50, true)[0][0]
|
||||
[255, 0, 0]
|
||||
> getRGBData(800, 600, -0.6, 0, 3.2, 50, true)[400][300]
|
||||
[0, 0, 0]
|
||||
*/
|
||||
|
||||
/**
|
||||
* Method to generate the image of the Mandelbrot set. Two types of coordinates are used:
|
||||
* image-coordinates that refer to the pixels and figure-coordinates that refer to the complex
|
||||
* numbers inside and outside the Mandelbrot set. The figure-coordinates in the arguments of this
|
||||
* method determine which section of the Mandelbrot set is viewed. The main area of the Mandelbrot
|
||||
* set is roughly between "-1.5 < x < 0.5" and "-1 < y < 1" in the figure-coordinates.
|
||||
* Method to generate the image of the Mandelbrot set.
|
||||
*
|
||||
* Two types of coordinates are used: image-coordinates that refer to the pixels and figure-coordinates that refer to
|
||||
* the complex numbers inside and outside the Mandelbrot set. The figure-coordinates in the arguments of this method
|
||||
* determine which section of the Mandelbrot set is viewed. The main area of the Mandelbrot set is roughly between
|
||||
* "-1.5 < x < 0.5" and "-1 < y < 1" in the figure-coordinates.
|
||||
*
|
||||
* The Mandelbrot set is the set of complex numbers "c" for which the series "z_(n+1) = z_n * z_n + c" does not diverge,
|
||||
* i.e. remains bounded. Thus, a complex number "c" is a member of the Mandelbrot set if, when starting with "z_0 = 0"
|
||||
* and applying the iteration repeatedly, the absolute value of "z_n" remains bounded for all "n > 0". Complex numbers
|
||||
* can be written as "a + b*i": "a" is the real component, usually drawn on the x-axis, and "b*i" is the imaginary
|
||||
* component, usually drawn on the y-axis. Most visualizations of the Mandelbrot set use a color-coding to indicate
|
||||
* after how many steps in the series the numbers outside the set cross the divergence threshold. Images of the
|
||||
* Mandelbrot set exhibit an elaborate and infinitely complicated boundary that reveals progressively ever-finer
|
||||
* recursive detail at increasing magnifications, making the boundary of the Mandelbrot set a fractal curve.
|
||||
*
|
||||
* (description adapted from https://en.wikipedia.org/wiki/Mandelbrot_set)
|
||||
* @see https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set
|
||||
*
|
||||
* @param {number} imageWidth The width of the rendered image.
|
||||
* @param {number} imageHeight The height of the rendered image.
|
||||
@@ -45,10 +24,10 @@ Pixel inside the Mandelbrot set should be black.
|
||||
* @param {number} figureCenterY The y-coordinate of the center of the figure.
|
||||
* @param {number} figureWidth The width of the figure.
|
||||
* @param {number} maxStep Maximum number of steps to check for divergent behavior.
|
||||
* @param {number} useDistanceColorCoding Render in color or black and white.
|
||||
* @param {boolean} useDistanceColorCoding Render in color or black and white.
|
||||
* @return {object} The RGB-data of the rendered Mandelbrot set.
|
||||
*/
|
||||
function getRGBData (
|
||||
export function getRGBData (
|
||||
imageWidth = 800,
|
||||
imageHeight = 600,
|
||||
figureCenterX = -0.6,
|
||||
@@ -83,9 +62,9 @@ function getRGBData (
|
||||
|
||||
// color the corresponding pixel based on the selected coloring-function
|
||||
rgbData[imageX][imageY] =
|
||||
useDistanceColorCoding
|
||||
? colorCodedColorMap(distance)
|
||||
: blackAndWhiteColorMap(distance)
|
||||
useDistanceColorCoding
|
||||
? colorCodedColorMap(distance)
|
||||
: blackAndWhiteColorMap(distance)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,8 +72,9 @@ function getRGBData (
|
||||
}
|
||||
|
||||
/**
|
||||
* Black and white color-coding that ignores the relative distance. The Mandelbrot set is black,
|
||||
* everything else is white.
|
||||
* Black and white color-coding that ignores the relative distance.
|
||||
*
|
||||
* The Mandelbrot set is black, everything else is white.
|
||||
*
|
||||
* @param {number} distance Distance until divergence threshold
|
||||
* @return {object} The RGB-value corresponding to the distance.
|
||||
@@ -104,7 +84,9 @@ function blackAndWhiteColorMap (distance) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Color-coding taking the relative distance into account. The Mandelbrot set is black.
|
||||
* Color-coding taking the relative distance into account.
|
||||
*
|
||||
* The Mandelbrot set is black.
|
||||
*
|
||||
* @param {number} distance Distance until divergence threshold
|
||||
* @return {object} The RGB-value corresponding to the distance.
|
||||
@@ -145,11 +127,12 @@ function colorCodedColorMap (distance) {
|
||||
|
||||
/**
|
||||
* Return the relative distance (ratio of steps taken to maxStep) after which the complex number
|
||||
* constituted by this x-y-pair diverges. Members of the Mandelbrot set do not diverge so their
|
||||
* distance is 1.
|
||||
* constituted by this x-y-pair diverges.
|
||||
*
|
||||
* Members of the Mandelbrot set do not diverge so their distance is 1.
|
||||
*
|
||||
* @param {number} figureX The x-coordinate within the figure.
|
||||
* @param {number} figureX The y-coordinate within the figure.
|
||||
* @param {number} figureY The y-coordinate within the figure.
|
||||
* @param {number} maxStep Maximum number of steps to check for divergent behavior.
|
||||
* @return {number} The relative distance as the ratio of steps taken to maxStep.
|
||||
*/
|
||||
@@ -171,22 +154,3 @@ function getDistance (figureX, figureY, maxStep) {
|
||||
}
|
||||
return currentStep / (maxStep - 1)
|
||||
}
|
||||
|
||||
// plot the results if the script is executed in a browser with a window-object
|
||||
if (typeof window !== 'undefined') {
|
||||
const rgbData = getRGBData()
|
||||
const width = rgbData.length
|
||||
const height = rgbData[0].length
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
const ctx = canvas.getContext('2d')
|
||||
for (let x = 0; x < width; x++) {
|
||||
for (let y = 0; y < height; y++) {
|
||||
const rgb = rgbData[x][y]
|
||||
ctx.fillStyle = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')'
|
||||
ctx.fillRect(x, y, 1, 1)
|
||||
}
|
||||
}
|
||||
document.body.append(canvas)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py
|
||||
*/
|
||||
|
||||
const PrimeFactors = (n) => {
|
||||
export const PrimeFactors = (n) => {
|
||||
// input: n: int
|
||||
// output: primeFactors: Array of all prime factors of n
|
||||
const primeFactors = []
|
||||
@@ -20,14 +20,3 @@ const PrimeFactors = (n) => {
|
||||
}
|
||||
return primeFactors
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
// PrimeFactors(100)
|
||||
// > [ 2, 2, 5, 5 ]
|
||||
console.log(PrimeFactors(100))
|
||||
// PrimeFactors(2560)
|
||||
// > [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 5 ]
|
||||
console.log(PrimeFactors(2560))
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
11
Maths/test/BinaryExponentiationRecursive.test.js
Normal file
11
Maths/test/BinaryExponentiationRecursive.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const { binaryExponentiation } = require('../BinaryExponentiationRecursive')
|
||||
|
||||
describe('BinaryExponentiationRecursive', () => {
|
||||
it('should calculate 2 to the power of 10 correctly', () => {
|
||||
expect(binaryExponentiation(2, 10)).toBe(1024)
|
||||
})
|
||||
|
||||
it('should calculate 3 to the power of 9 correctly', () => {
|
||||
expect(binaryExponentiation(3, 9)).toBe(19683)
|
||||
})
|
||||
})
|
||||
66
Maths/test/EulerMethod.manual-test.js
Normal file
66
Maths/test/EulerMethod.manual-test.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { eulerFull } from '../EulerMethod'
|
||||
|
||||
function plotLine (label, points, width, height) {
|
||||
// utility function to plot the results
|
||||
|
||||
// container needed to control the size of the canvas
|
||||
const container = document.createElement('div')
|
||||
container.style.width = width + 'px'
|
||||
container.style.height = height + 'px'
|
||||
document.body.append(container)
|
||||
|
||||
// the canvas for plotting
|
||||
const canvas = document.createElement('canvas')
|
||||
container.append(canvas)
|
||||
|
||||
// Chart-class from chartjs
|
||||
const chart = new Chart(canvas, { // eslint-disable-line
|
||||
type: 'scatter',
|
||||
data: {
|
||||
datasets: [{
|
||||
label: label,
|
||||
data: points,
|
||||
showLine: true,
|
||||
fill: false,
|
||||
tension: 0,
|
||||
borderColor: 'black'
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
maintainAspectRatio: false,
|
||||
responsive: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function exampleEquation1 (x, y) {
|
||||
return x
|
||||
}
|
||||
|
||||
// example from https://en.wikipedia.org/wiki/Euler_method
|
||||
function exampleEquation2 (x, y) {
|
||||
return y
|
||||
}
|
||||
|
||||
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
||||
function exampleEquation3 (x, y) {
|
||||
return x + y + x * y
|
||||
}
|
||||
|
||||
// plot the results if the script is executed in a browser with a window-object
|
||||
if (typeof window !== 'undefined') {
|
||||
const points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1)
|
||||
const points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2)
|
||||
const points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3)
|
||||
|
||||
const script = document.createElement('script')
|
||||
|
||||
// using chartjs
|
||||
script.src = 'https://www.chartjs.org/dist/2.9.4/Chart.min.js'
|
||||
script.onload = function () {
|
||||
plotLine('example 1: dy/dx = x', points1, 600, 400)
|
||||
plotLine('example 2: dy/dx = y', points2, 600, 400)
|
||||
plotLine('example 3: dy/dx = x + y + x * y', points3, 600, 400)
|
||||
}
|
||||
document.body.append(script)
|
||||
}
|
||||
18
Maths/test/EulerMethod.test.js
Normal file
18
Maths/test/EulerMethod.test.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { eulerFull, eulerStep } from '../EulerMethod'
|
||||
|
||||
describe('eulerStep', () => {
|
||||
it('should calculate the next y value correctly', () => {
|
||||
expect(eulerStep(0, 0.1, 0, function (x, y) { return x })).toBe(0)
|
||||
expect(eulerStep(2, 1, 1, function (x, y) { return x * x })).toBe(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('eulerFull', () => {
|
||||
it('should return all the points found', () => {
|
||||
expect(eulerFull(0, 3, 1, 0, function (x, y) { return x }))
|
||||
.toEqual([{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 1 }, { x: 3, y: 3 }])
|
||||
|
||||
expect(eulerFull(3, 4, 0.5, 1, function (x, y) { return x * x }))
|
||||
.toEqual([{ x: 3, y: 1 }, { x: 3.5, y: 5.5 }, { x: 4, y: 11.625 }])
|
||||
})
|
||||
})
|
||||
11
Maths/test/EulersTotient.test.js
Normal file
11
Maths/test/EulersTotient.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { EulersTotient } from '../EulersTotient'
|
||||
|
||||
describe('EulersTotient', () => {
|
||||
it('should return 6 as 1, 2, 4, 5, 7, and 8 are coprime to 9', () => {
|
||||
expect(EulersTotient(9)).toBe(6)
|
||||
})
|
||||
|
||||
it('should return 4 as 1, 3, 7, and 9 are coprime to 10', () => {
|
||||
expect(EulersTotient(10)).toBe(4)
|
||||
})
|
||||
})
|
||||
20
Maths/test/Mandelbrot.manual-test.js
Normal file
20
Maths/test/Mandelbrot.manual-test.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { getRGBData } from '../Mandelbrot'
|
||||
|
||||
// plot the results if the script is executed in a browser with a window-object
|
||||
if (typeof window !== 'undefined') {
|
||||
const rgbData = getRGBData()
|
||||
const width = rgbData.length
|
||||
const height = rgbData[0].length
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
const ctx = canvas.getContext('2d')
|
||||
for (let x = 0; x < width; x++) {
|
||||
for (let y = 0; y < height; y++) {
|
||||
const rgb = rgbData[x][y]
|
||||
ctx.fillStyle = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')'
|
||||
ctx.fillRect(x, y, 1, 1)
|
||||
}
|
||||
}
|
||||
document.body.append(canvas)
|
||||
}
|
||||
21
Maths/test/Mandelbrot.test.js
Normal file
21
Maths/test/Mandelbrot.test.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getRGBData } from '../Mandelbrot'
|
||||
|
||||
describe('Mandelbrot', () => {
|
||||
it('should produce black pixels inside the set', () => {
|
||||
const blackAndWhite = getRGBData(800, 600, -0.6, 0, 3.2, 50, false)
|
||||
expect(blackAndWhite[400][300]).toEqual([0, 0, 0]) // black
|
||||
|
||||
const colorCoded = getRGBData(800, 600, -0.6, 0, 3.2, 50, true)
|
||||
expect(colorCoded[400][300]).toEqual([0, 0, 0]) // black
|
||||
})
|
||||
|
||||
it('should produce white pixels outside of the set', () => {
|
||||
const blackAndWhite = getRGBData(800, 600, -0.6, 0, 3.2, 50, false)
|
||||
expect(blackAndWhite[0][0]).toEqual([255, 255, 255]) // black
|
||||
})
|
||||
|
||||
it('should produce colored pixels distant to the set', () => {
|
||||
const colorCoded = getRGBData(800, 600, -0.6, 0, 3.2, 50, true)
|
||||
expect(colorCoded[0][0]).toEqual([255, 0, 0]) // red
|
||||
})
|
||||
})
|
||||
11
Maths/test/PrimeFactors.test.js
Normal file
11
Maths/test/PrimeFactors.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { PrimeFactors } from '../PrimeFactors'
|
||||
|
||||
describe('EulersTotient', () => {
|
||||
it('should return the prime factors for 100', () => {
|
||||
expect(PrimeFactors(100)).toEqual([2, 2, 5, 5])
|
||||
})
|
||||
|
||||
it('should return the prime factors for 2560', () => {
|
||||
expect(PrimeFactors(2560)).toEqual([2, 2, 2, 2, 2, 2, 2, 2, 2, 5])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user