|
| 1 | +import { readFileSync } from "fs"; |
| 2 | +import test from "flug"; |
| 3 | +import max from "./index"; |
| 4 | + |
| 5 | +test("gettings maximum from a normal array", ({ eq }) => { |
| 6 | + const numbers = [920, 550, 340, 690, 550, 340, 840, 700, 550, 210, 540]; |
| 7 | + const result = max(numbers, { debug: false }); |
| 8 | + eq(result, 920); |
| 9 | +}); |
| 10 | + |
| 11 | +test("getting maximum from an array of image band values", ({ eq }) => { |
| 12 | + const numbers = Uint8Array.from(JSON.parse(readFileSync("uint8-numbers.json", "utf-8"))); |
| 13 | + console.log("loaded uint8 numbers of length:", numbers.length); |
| 14 | + const result = max(numbers, { debug: false }); |
| 15 | + eq(result, 255); |
| 16 | +}); |
| 17 | + |
| 18 | +test("setting theoretical maximum", ({ eq }) => { |
| 19 | + const numbers = Array.from(Uint8Array.from(JSON.parse(readFileSync("uint8-numbers.json", "utf-8")))); |
| 20 | + console.log("loaded uint8 numbers of length:", numbers.length); |
| 21 | + const result = max(numbers, { debug: false, theoretical_max: 255 }); |
| 22 | + eq(result, 255); |
| 23 | +}); |
| 24 | + |
| 25 | +test("getting maximum from typed arrays", ({ eq }) => { |
| 26 | + [ |
| 27 | + [Int8Array, 127] as const, |
| 28 | + [Uint8Array, 255] as const, |
| 29 | + [Int16Array, 32767] as const, |
| 30 | + [Uint16Array, 65535] as const, |
| 31 | + ].forEach(([array_type, expected_max]) => { |
| 32 | + const filename = array_type.name.replace("Array", "").toLowerCase() + "-numbers.json"; |
| 33 | + const numbers = array_type.from(JSON.parse(readFileSync(filename, "utf-8"))); |
| 34 | + const result = max(numbers, { debug: false }); |
| 35 | + eq(result, expected_max); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +test("getting maximum from a very large untyped array", ({ eq }) => { |
| 40 | + const numbers = JSON.parse(readFileSync("uint16-numbers.json", "utf-8")).map(n => Number(n)); |
| 41 | + const result = max(numbers, { debug: true }); |
| 42 | + eq(result, 65535); |
| 43 | +}); |
| 44 | + |
| 45 | +test("getting no maximum from normal arrays with all no data values", ({ eq }) => { |
| 46 | + const numbers = [99, 99, 99, 99]; |
| 47 | + const result = max(numbers, { no_data: 99 }); |
| 48 | + eq(result, undefined); |
| 49 | +}); |
| 50 | + |
| 51 | +test("getting maximum from normal arrays with some data values", ({ eq }) => { |
| 52 | + const numbers = [1, 99, 2, 99, 4, 99, 6, 99, -10]; |
| 53 | + const result = max(numbers, { no_data: 99 }); |
| 54 | + eq(result, 6); |
| 55 | +}); |
| 56 | + |
| 57 | +test("getting no maximum from typed arrays with all no data values", ({ eq }) => { |
| 58 | + const numbers = Uint8Array.from([99, 99, 99, 99]); |
| 59 | + const result = max(numbers, { no_data: 99 }); |
| 60 | + eq(result, undefined); |
| 61 | +}); |
| 62 | + |
| 63 | +test("getting maximum from typed arrays with some data values", ({ eq }) => { |
| 64 | + const numbers = Int8Array.from([1, 99, 2, 99, 4, 99, 6, 99, -10]); |
| 65 | + const result = max(numbers, { no_data: 99 }); |
| 66 | + eq(result, 6); |
| 67 | +}); |
0 commit comments