Fix hashCode/equals inconsistency for negative zero in Duration and DataSize (#1932)
This commit is contained in:
1 parent
3f5797de6f
commit
dd83b9ab4f
4 files changed
+16
-4
No files matched your search
@@ -247,7 +247,8 @@ public final class DataSize implements Value {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Double.hashCode(convertValueTo(DataSizeUnit.BYTES));
|
// adding 0.0 normalizes -0.0 to 0.0, because -0.0 == 0.0 but their hash codes differ
|
||||||
|
return Double.hashCode(convertValueTo(DataSizeUnit.BYTES) + 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -222,7 +222,8 @@ public final class Duration implements Value {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Double.hashCode(convertValueTo(DurationUnit.NANOS));
|
// adding 0.0 normalizes -0.0 to 0.0, because -0.0 == 0.0 but their hash codes differ
|
||||||
|
return Double.hashCode(convertValueTo(DurationUnit.NANOS) + 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -121,5 +121,10 @@ class DataSizeTest {
|
|||||||
|
|
||||||
assertThat(size3.hashCode()).isNotEqualTo(size1.hashCode())
|
assertThat(size3.hashCode()).isNotEqualTo(size1.hashCode())
|
||||||
assertThat(size2.hashCode()).isNotEqualTo(size3.hashCode())
|
assertThat(size2.hashCode()).isNotEqualTo(size3.hashCode())
|
||||||
|
|
||||||
|
// -0.0 == 0.0, so equal sizes must have equal hash codes
|
||||||
|
assertThat(DataSize(0.0, BYTES)).isEqualTo(DataSize(-0.0, BYTES))
|
||||||
|
assertThat(DataSize(0.0, BYTES).hashCode()).isEqualTo(DataSize(-0.0, BYTES).hashCode())
|
||||||
|
assertThat(DataSize(0.0, BYTES).hashCode()).isEqualTo(DataSize(-0.0, PEBIBYTES).hashCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -285,5 +285,10 @@ class DurationTest {
|
|||||||
|
|
||||||
assertThat(duration3.hashCode()).isNotEqualTo(duration1.hashCode())
|
assertThat(duration3.hashCode()).isNotEqualTo(duration1.hashCode())
|
||||||
assertThat(duration2.hashCode()).isNotEqualTo(duration3.hashCode())
|
assertThat(duration2.hashCode()).isNotEqualTo(duration3.hashCode())
|
||||||
|
|
||||||
|
// -0.0 == 0.0, so equal durations must have equal hash codes
|
||||||
|
assertThat(Duration(0.0, MILLIS)).isEqualTo(Duration(-0.0, MILLIS))
|
||||||
|
assertThat(Duration(0.0, MILLIS).hashCode()).isEqualTo(Duration(-0.0, MILLIS).hashCode())
|
||||||
|
assertThat(Duration(0.0, NANOS).hashCode()).isEqualTo(Duration(-0.0, DAYS).hashCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user