|
| 1 | +/** |
| 2 | + * Copyright 2023 University of Adelaide |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 18 | + |
| 19 | +import { acceptWorseWithThres } from "@/optimizer/optimizer.helper.class"; |
| 20 | + |
| 21 | +let originalRandom: () => number; |
| 22 | + |
| 23 | +beforeAll(() => { |
| 24 | + originalRandom = Math.random; |
| 25 | +}); |
| 26 | +afterAll(() => { |
| 27 | + Math.random = originalRandom; |
| 28 | +}); |
| 29 | + |
| 30 | +describe("acceptWorseWithThres", () => { |
| 31 | + it("should return true if new is smaller than old", () => { |
| 32 | + expect(acceptWorseWithThres(10, 20, -1)).toBeTruthy(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should likely return true thres=auto and new approx old", () => { |
| 36 | + Math.random = () => 0.1; |
| 37 | + expect(acceptWorseWithThres(21, 20, -1)).toBeTruthy(); |
| 38 | + }); |
| 39 | + it("should likely return false thres=auto and new far off old", () => { |
| 40 | + Math.random = () => 0.1; |
| 41 | + expect(acceptWorseWithThres(200, 20, -1)).toBeFalsy(); |
| 42 | + }); |
| 43 | + it("should likely return true if le than thres, independent of how much worse new is", () => { |
| 44 | + Math.random = () => 0.1; |
| 45 | + expect(acceptWorseWithThres(21, 20, 0.1)).toBeTruthy(); |
| 46 | + expect(acceptWorseWithThres(22, 20, 0.1)).toBeTruthy(); |
| 47 | + expect(acceptWorseWithThres(2200, 20, 0.1)).toBeTruthy(); |
| 48 | + }); |
| 49 | + it("should likely return false if higher than thres, independent of how much worse new is", () => { |
| 50 | + Math.random = () => 0.2; |
| 51 | + expect(acceptWorseWithThres(21, 20, 0.1)).toBeFalsy(); |
| 52 | + expect(acceptWorseWithThres(22, 20, 0.1)).toBeFalsy(); |
| 53 | + expect(acceptWorseWithThres(2200, 20, 0.1)).toBeFalsy(); |
| 54 | + }); |
| 55 | + it("should not return true if thres is 0 and the new one is worse (default)", () => { |
| 56 | + Math.random = () => 0.2; |
| 57 | + expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy(); |
| 58 | + expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy(); |
| 59 | + expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy(); |
| 60 | + Math.random = () => 0.1; |
| 61 | + expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy(); |
| 62 | + expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy(); |
| 63 | + expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy(); |
| 64 | + Math.random = () => 0.00001; |
| 65 | + expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy(); |
| 66 | + expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy(); |
| 67 | + expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy(); |
| 68 | + Math.random = () => 0.9; |
| 69 | + expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy(); |
| 70 | + expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy(); |
| 71 | + expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments