summaryrefslogtreecommitdiffstats
path: root/vendor/bytesize
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/bytesize
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/bytesize')
-rw-r--r--vendor/bytesize/.cargo-checksum.json1
-rw-r--r--vendor/bytesize/Cargo.toml27
-rw-r--r--vendor/bytesize/LICENSE202
-rw-r--r--vendor/bytesize/README.md95
-rw-r--r--vendor/bytesize/src/lib.rs377
5 files changed, 702 insertions, 0 deletions
diff --git a/vendor/bytesize/.cargo-checksum.json b/vendor/bytesize/.cargo-checksum.json
new file mode 100644
index 000000000..654da467e
--- /dev/null
+++ b/vendor/bytesize/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"151e2d4b44c05fa3c40d21ca163c954c53592db56153c039c128f0ebd9bfbe79","LICENSE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","README.md":"f0716240034443c0407f8e1e9f88c6db76ad7f41273de491bbc03743ca1cb5b8","src/lib.rs":"14bfe436ff5a786d718b9f60e5b6428a5899bfa97a307814b0973ce5320fc59e"},"package":"81a18687293a1546b67c246452202bbbf143d239cb43494cc163da14979082da"} \ No newline at end of file
diff --git a/vendor/bytesize/Cargo.toml b/vendor/bytesize/Cargo.toml
new file mode 100644
index 000000000..5265e11b9
--- /dev/null
+++ b/vendor/bytesize/Cargo.toml
@@ -0,0 +1,27 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+name = "bytesize"
+version = "1.0.1"
+authors = ["Hyunsik Choi <hyunsik.choi@gmail.com>"]
+description = "an utility for human-readable bytes representations"
+homepage = "https://github.com/hyunsik/bytesize/"
+documentation = "https://docs.rs/bytesize/"
+readme = "README.md"
+keywords = ["byte", "byte-size", "utility", "human-readable", "format"]
+license = "Apache-2.0"
+repository = "https://github.com/hyunsik/bytesize/"
+[dependencies.serde]
+version = "1.0"
+features = ["derive"]
+optional = true
diff --git a/vendor/bytesize/LICENSE b/vendor/bytesize/LICENSE
new file mode 100644
index 000000000..8f71f43fe
--- /dev/null
+++ b/vendor/bytesize/LICENSE
@@ -0,0 +1,202 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
diff --git a/vendor/bytesize/README.md b/vendor/bytesize/README.md
new file mode 100644
index 000000000..9c3464d0e
--- /dev/null
+++ b/vendor/bytesize/README.md
@@ -0,0 +1,95 @@
+## ByteSize
+[![Build Status](https://travis-ci.org/hyunsik/bytesize.svg?branch=master)](https://travis-ci.org/hyunsik/bytesize)
+[![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize)
+
+
+ByteSize is an utility for human-readable byte count representation.
+
+[API Documentation](https://docs.rs/bytesize/)
+
+## Usage
+
+Add this to your Cargo.toml:
+
+```toml
+[dependencies]
+bytesize = "1.0.0"
+```
+
+and this to your crate root:
+```rust
+extern crate bytesize;
+```
+
+## Example
+### Human readable representations (SI unit and Binary unit)
+```rust
+#[allow(dead_code)]
+fn assert_display(expected: &str, b: ByteSize) {
+ assert_eq!(expected, format!("{}", b));
+}
+
+#[test]
+ fn test_display() {
+ assert_display("215 B", ByteSize(215));
+ assert_display("215 B", ByteSize::b(215));
+ assert_display("1.0 KB", ByteSize::kb(1));
+ assert_display("301.0 KB", ByteSize::kb(301));
+ assert_display("419.0 MB", ByteSize::mb(419));
+ assert_display("518.0 GB", ByteSize::gb(518));
+ assert_display("815.0 TB", ByteSize::tb(815));
+ assert_display("609.0 PB", ByteSize::pb(609));
+ }
+
+ fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
+ assert_eq!(expected.to_string(), b.to_string_as(si));
+ }
+
+ #[test]
+ fn test_to_string() {
+ assert_to_string("215 B", ByteSize(215), true);
+ assert_to_string("215 B", ByteSize(215), false);
+
+ assert_to_string("215 B", ByteSize::b(215), true);
+ assert_to_string("215 B", ByteSize::b(215), false);
+
+ assert_to_string("1.0 kiB", ByteSize::kib(1), true);
+ assert_to_string("1.0 KB", ByteSize::kib(1), false);
+
+ assert_to_string("293.9 kiB", ByteSize::kb(301), true);
+ assert_to_string("301.0 KB", ByteSize::kb(301), false);
+
+ assert_to_string("1.0 MiB", ByteSize::mib(1), true);
+ assert_to_string("1048.6 KB", ByteSize::mib(1), false);
+
+ assert_to_string("399.6 MiB", ByteSize::mb(419), true);
+ assert_to_string("419.0 MB", ByteSize::mb(419), false);
+
+ assert_to_string("482.4 GiB", ByteSize::gb(518), true);
+ assert_to_string("518.0 GB", ByteSize::gb(518), false);
+
+ assert_to_string("741.2 TiB", ByteSize::tb(815), true);
+ assert_to_string("815.0 TB", ByteSize::tb(815), false);
+
+ assert_to_string("540.9 PiB", ByteSize::pb(609), true);
+ assert_to_string("609.0 PB", ByteSize::pb(609), false);
+}
+```
+
+### Arithmetic operations
+```rust
+extern crate bytesize;
+
+use bytesize::ByteSize;
+
+fn byte_arithmetic_operator() {
+ let x = ByteSize::mb(1);
+ let y = ByteSize::kb(100);
+
+ let plus = x + y;
+ print!("{}", plus);
+
+ let minus = ByteSize::tb(100) + ByteSize::gb(4);
+ print!("{}", minus);
+}
+```
diff --git a/vendor/bytesize/src/lib.rs b/vendor/bytesize/src/lib.rs
new file mode 100644
index 000000000..e2d63c9c5
--- /dev/null
+++ b/vendor/bytesize/src/lib.rs
@@ -0,0 +1,377 @@
+//! ByteSize is an utility that easily makes bytes size representation
+//! and helps its arithmetic operations.
+//!
+//! ## Example
+//!
+//! ```ignore
+//! extern crate bytesize;
+//!
+//! use bytesize::ByteSize;
+//!
+//! fn byte_arithmetic_operator() {
+//! let x = ByteSize::mb(1);
+//! let y = ByteSize::kb(100);
+//!
+//! let plus = x + y;
+//! print!("{} bytes", plus.as_u64());
+//!
+//! let minus = ByteSize::tb(100) - ByteSize::gb(4);
+//! print!("{} bytes", minus.as_u64());
+//! }
+//! ```
+//!
+//! It also provides its human readable string as follows:
+//!
+//! ```ignore=
+//! assert_eq!("482 GiB".to_string(), ByteSize::gb(518).to_string(true));
+//! assert_eq!("518 GB".to_string(), ByteSize::gb(518).to_string(false));
+//! ```
+
+#[cfg(feature = "serde")]
+#[macro_use]
+extern crate serde;
+
+use std::fmt::{Debug, Display, Formatter, Result};
+use std::ops::{Add, Mul};
+
+/// byte size for 1 byte
+pub const B: u64 = 1;
+/// bytes size for 1 kilobyte
+pub const KB: u64 = 1_000;
+/// bytes size for 1 megabyte
+pub const MB: u64 = 1_000_000;
+/// bytes size for 1 gigabyte
+pub const GB: u64 = 1_000_000_000;
+/// bytes size for 1 terabyte
+pub const TB: u64 = 1_000_000_000_000;
+/// bytes size for 1 petabyte
+pub const PB: u64 = 1_000_000_000_000_000;
+
+/// bytes size for 1 kibibyte
+pub const KIB: u64 = 1_024;
+/// bytes size for 1 mebibyte
+pub const MIB: u64 = 1_048_576;
+/// bytes size for 1 gibibyte
+pub const GIB: u64 = 1_073_741_824;
+/// bytes size for 1 tebibyte
+pub const TIB: u64 = 1_099_511_627_776;
+/// bytes size for 1 pebibyte
+pub const PIB: u64 = 1_125_899_906_842_624;
+
+static UNITS: &'static str = "KMGTPE";
+static UNITS_SI: &'static str = "kMGTPE";
+static LN_KB: f64 = 6.931471806; // ln 1024
+static LN_KIB: f64 = 6.907755279; // ln 1000
+
+pub fn kb<V: Into<u64>>(size: V) -> u64 {
+ size.into() * KB
+}
+
+pub fn kib<V: Into<u64>>(size: V) -> u64 {
+ size.into() * KIB
+}
+
+pub fn mb<V: Into<u64>>(size: V) -> u64 {
+ size.into() * MB
+}
+
+pub fn mib<V: Into<u64>>(size: V) -> u64 {
+ size.into() * MIB
+}
+
+pub fn gb<V: Into<u64>>(size: V) -> u64 {
+ size.into() * GB
+}
+
+pub fn gib<V: Into<u64>>(size: V) -> u64 {
+ size.into() * GIB
+}
+
+pub fn tb<V: Into<u64>>(size: V) -> u64 {
+ size.into() * TB
+}
+
+pub fn tib<V: Into<u64>>(size: V) -> u64 {
+ size.into() * TIB
+}
+
+pub fn pb<V: Into<u64>>(size: V) -> u64 {
+ size.into() * PB
+}
+
+pub fn pib<V: Into<u64>>(size: V) -> u64 {
+ size.into() * PIB
+}
+
+/// Byte size representation
+#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Default)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+pub struct ByteSize(pub u64);
+
+impl ByteSize {
+ #[inline(always)]
+ pub fn b(size: u64) -> ByteSize {
+ ByteSize(size)
+ }
+
+ #[inline(always)]
+ pub fn kb(size: u64) -> ByteSize {
+ ByteSize(size * KB)
+ }
+
+ #[inline(always)]
+ pub fn kib(size: u64) -> ByteSize {
+ ByteSize(size * KIB)
+ }
+
+ #[inline(always)]
+ pub fn mb(size: u64) -> ByteSize {
+ ByteSize(size * MB)
+ }
+
+ #[inline(always)]
+ pub fn mib(size: u64) -> ByteSize {
+ ByteSize(size * MIB)
+ }
+
+ #[inline(always)]
+ pub fn gb(size: u64) -> ByteSize {
+ ByteSize(size * GB)
+ }
+
+ #[inline(always)]
+ pub fn gib(size: u64) -> ByteSize {
+ ByteSize(size * GIB)
+ }
+
+ #[inline(always)]
+ pub fn tb(size: u64) -> ByteSize {
+ ByteSize(size * TB)
+ }
+
+ #[inline(always)]
+ pub fn tib(size: u64) -> ByteSize {
+ ByteSize(size * TIB)
+ }
+
+ #[inline(always)]
+ pub fn pb(size: u64) -> ByteSize {
+ ByteSize(size * PB)
+ }
+
+ #[inline(always)]
+ pub fn pib(size: u64) -> ByteSize {
+ ByteSize(size * PIB)
+ }
+
+ #[inline(always)]
+ pub fn as_u64(&self) -> u64 {
+ self.0
+ }
+
+ #[inline(always)]
+ pub fn to_string_as(&self, si_unit: bool) -> String {
+ to_string(self.0, si_unit)
+ }
+}
+
+pub fn to_string(bytes: u64, si_prefix: bool) -> String {
+ let unit = if si_prefix { KIB } else { KB };
+ let unit_base = if si_prefix { LN_KIB } else { LN_KB };
+ let unit_prefix = if si_prefix {
+ UNITS_SI.as_bytes()
+ } else {
+ UNITS.as_bytes()
+ };
+ let unit_suffix = if si_prefix { "iB" } else { "B" };
+
+ if bytes < unit {
+ format!("{} B", bytes)
+ } else {
+ let size = bytes as f64;
+ let exp = match (size.ln() / unit_base) as usize {
+ e if e == 0 => 1,
+ e => e,
+ };
+
+ format!(
+ "{:.1} {}{}",
+ (size / unit.pow(exp as u32) as f64),
+ unit_prefix[exp - 1] as char,
+ unit_suffix
+ )
+ }
+}
+
+impl Display for ByteSize {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.pad(&to_string(self.0, false))
+ }
+}
+
+impl Debug for ByteSize {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ write!(f, "{}", self)
+ }
+}
+
+macro_rules! commutative_op {
+ ($t:ty) => {
+ impl Add<$t> for ByteSize {
+ type Output = ByteSize;
+ #[inline(always)]
+ fn add(self, rhs: $t) -> ByteSize {
+ ByteSize(self.0 + (rhs as u64))
+ }
+ }
+
+ impl Add<ByteSize> for $t {
+ type Output = ByteSize;
+ #[inline(always)]
+ fn add(self, rhs: ByteSize) -> ByteSize {
+ ByteSize(rhs.0 + (self as u64))
+ }
+ }
+
+ impl Mul<$t> for ByteSize {
+ type Output = ByteSize;
+ #[inline(always)]
+ fn mul(self, rhs: $t) -> ByteSize {
+ ByteSize(self.0 * (rhs as u64))
+ }
+ }
+
+ impl Mul<ByteSize> for $t {
+ type Output = ByteSize;
+ #[inline(always)]
+ fn mul(self, rhs: ByteSize) -> ByteSize {
+ ByteSize(rhs.0 * (self as u64))
+ }
+ }
+ };
+}
+
+commutative_op!(u64);
+commutative_op!(u32);
+commutative_op!(u16);
+commutative_op!(u8);
+
+impl Add<ByteSize> for ByteSize {
+ type Output = ByteSize;
+
+ #[inline(always)]
+ fn add(self, rhs: ByteSize) -> ByteSize {
+ ByteSize(self.0 + rhs.0)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_arithmetic_op() {
+ let x = ByteSize::mb(1);
+ let y = ByteSize::kb(100);
+
+ assert_eq!((x + y).as_u64(), 1_100_000u64);
+
+ assert_eq!((x + (100 * 1000) as u64).as_u64(), 1_100_000);
+
+ assert_eq!((x * 2u64).as_u64(), 2_000_000);
+ }
+
+ #[test]
+ fn test_arithmetic_primitives() {
+ let x = ByteSize::mb(1);
+
+ assert_eq!((x + MB as u64).as_u64(), 2_000_000);
+
+ assert_eq!((x + MB as u32).as_u64(), 2_000_000);
+
+ assert_eq!((x + KB as u16).as_u64(), 1_001_000);
+
+ assert_eq!((x + B as u8).as_u64(), 1_000_001);
+ }
+
+ #[test]
+ fn test_comparison() {
+ assert!(ByteSize::mb(1) == ByteSize::kb(1000));
+ assert!(ByteSize::mib(1) == ByteSize::kib(1024));
+ assert!(ByteSize::mb(1) != ByteSize::kib(1024));
+ assert!(ByteSize::mb(1) < ByteSize::kib(1024));
+ assert!(ByteSize::b(0) < ByteSize::tib(1));
+ }
+
+ fn assert_display(expected: &str, b: ByteSize) {
+ assert_eq!(expected, format!("{}", b));
+ }
+
+ #[test]
+ fn test_display() {
+ assert_display("215 B", ByteSize::b(215));
+ assert_display("1.0 KB", ByteSize::kb(1));
+ assert_display("301.0 KB", ByteSize::kb(301));
+ assert_display("419.0 MB", ByteSize::mb(419));
+ assert_display("518.0 GB", ByteSize::gb(518));
+ assert_display("815.0 TB", ByteSize::tb(815));
+ assert_display("609.0 PB", ByteSize::pb(609));
+ }
+
+ #[test]
+ fn test_display_alignment() {
+ assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357)));
+ assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357)));
+ assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357)));
+ assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357)));
+
+ assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357)));
+ assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357)));
+ assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357)));
+ }
+
+ fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
+ assert_eq!(expected.to_string(), b.to_string_as(si));
+ }
+
+ #[test]
+ fn test_to_string_as() {
+ assert_to_string("215 B", ByteSize::b(215), true);
+ assert_to_string("215 B", ByteSize::b(215), false);
+
+ assert_to_string("1.0 kiB", ByteSize::kib(1), true);
+ assert_to_string("1.0 KB", ByteSize::kib(1), false);
+
+ assert_to_string("293.9 kiB", ByteSize::kb(301), true);
+ assert_to_string("301.0 KB", ByteSize::kb(301), false);
+
+ assert_to_string("1.0 MiB", ByteSize::mib(1), true);
+ assert_to_string("1048.6 KB", ByteSize::mib(1), false);
+
+ // a bug case: https://github.com/flang-project/bytesize/issues/8
+ assert_to_string("1.9 GiB", ByteSize::mib(1907), true);
+ assert_to_string("2.0 GB", ByteSize::mib(1908), false);
+
+ assert_to_string("399.6 MiB", ByteSize::mb(419), true);
+ assert_to_string("419.0 MB", ByteSize::mb(419), false);
+
+ assert_to_string("482.4 GiB", ByteSize::gb(518), true);
+ assert_to_string("518.0 GB", ByteSize::gb(518), false);
+
+ assert_to_string("741.2 TiB", ByteSize::tb(815), true);
+ assert_to_string("815.0 TB", ByteSize::tb(815), false);
+
+ assert_to_string("540.9 PiB", ByteSize::pb(609), true);
+ assert_to_string("609.0 PB", ByteSize::pb(609), false);
+ }
+
+ #[test]
+ fn test_default() {
+ assert_eq!(ByteSize::b(0), ByteSize::default());
+ }
+
+ #[test]
+ fn test_to_string() {
+ assert_to_string("609.0 PB", ByteSize::pb(609), false);
+ }
+}