mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-18 14:38:04 -06:00
WebUI: replace rounding function from MooTools
The `round()` returning floating point number is not a good idea. This is due to floating point representation is imprecise and sometimes it cannot faithfully represent a number, for example `0.09 + 0.01 !== 0.1 `. Therefore, it should be avoided and/or utilize other function to achieve the goal. Also, improve `window.qBittorrent.Misc.toFixedPointString()` and add test cases. PR #22281.
This commit is contained in:
78
src/webui/www/test/private/misc.test.js
Normal file
78
src/webui/www/test/private/misc.test.js
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2025 Mike Tzou (Chocobo1)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* In addition, as a special exception, the copyright holders give permission to
|
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||||
* modified versions of it that use the same license as the "OpenSSL" library),
|
||||
* and distribute the linked executables. You must obey the GNU General Public
|
||||
* License in all respects for all of the code used other than "OpenSSL". If you
|
||||
* modify file(s), you may extend this exception to your version of the file(s),
|
||||
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||||
* exception statement from your version.
|
||||
*/
|
||||
|
||||
import { expect, test } from "vitest";
|
||||
import "../../private/scripts/misc.js";
|
||||
|
||||
test("Test toFixedPointString()", () => {
|
||||
const toFixedPointString = window.qBittorrent.Misc.toFixedPointString;
|
||||
|
||||
expect(toFixedPointString(0, 0)).toBe("0");
|
||||
expect(toFixedPointString(0, 1)).toBe("0.0");
|
||||
expect(toFixedPointString(0, 2)).toBe("0.00");
|
||||
|
||||
expect(toFixedPointString(0.1, 0)).toBe("0");
|
||||
expect(toFixedPointString(0.1, 1)).toBe("0.1");
|
||||
expect(toFixedPointString(0.1, 2)).toBe("0.10");
|
||||
|
||||
expect(toFixedPointString(-0.1, 0)).toBe("0");
|
||||
expect(toFixedPointString(-0.1, 1)).toBe("-0.1");
|
||||
expect(toFixedPointString(-0.1, 2)).toBe("-0.10");
|
||||
|
||||
expect(toFixedPointString(1.005, 0)).toBe("1");
|
||||
expect(toFixedPointString(1.005, 1)).toBe("1.0");
|
||||
expect(toFixedPointString(1.005, 2)).toBe("1.00");
|
||||
expect(toFixedPointString(1.005, 3)).toBe("1.005");
|
||||
expect(toFixedPointString(1.005, 4)).toBe("1.0050");
|
||||
|
||||
expect(toFixedPointString(-1.005, 0)).toBe("-1");
|
||||
expect(toFixedPointString(-1.005, 1)).toBe("-1.0");
|
||||
expect(toFixedPointString(-1.005, 2)).toBe("-1.00");
|
||||
expect(toFixedPointString(-1.005, 3)).toBe("-1.005");
|
||||
expect(toFixedPointString(-1.005, 4)).toBe("-1.0050");
|
||||
|
||||
expect(toFixedPointString(35.855, 0)).toBe("35");
|
||||
expect(toFixedPointString(35.855, 1)).toBe("35.8");
|
||||
expect(toFixedPointString(35.855, 2)).toBe("35.85");
|
||||
expect(toFixedPointString(35.855, 3)).toBe("35.855");
|
||||
expect(toFixedPointString(35.855, 4)).toBe("35.8550");
|
||||
|
||||
expect(toFixedPointString(-35.855, 0)).toBe("-35");
|
||||
expect(toFixedPointString(-35.855, 1)).toBe("-35.8");
|
||||
expect(toFixedPointString(-35.855, 2)).toBe("-35.85");
|
||||
expect(toFixedPointString(-35.855, 3)).toBe("-35.855");
|
||||
expect(toFixedPointString(-35.855, 4)).toBe("-35.8550");
|
||||
|
||||
expect(toFixedPointString(100.00, 0)).toBe("100");
|
||||
expect(toFixedPointString(100.00, 1)).toBe("100.0");
|
||||
expect(toFixedPointString(100.00, 2)).toBe("100.00");
|
||||
|
||||
expect(toFixedPointString(-100.00, 0)).toBe("-100");
|
||||
expect(toFixedPointString(-100.00, 1)).toBe("-100.0");
|
||||
expect(toFixedPointString(-100.00, 2)).toBe("-100.00");
|
||||
});
|
||||
Reference in New Issue
Block a user