-
-
Notifications
You must be signed in to change notification settings - Fork 475
fix(android): Avoid Math floor APIs #5743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+131
−4
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cba95e0
fix(android): Avoid Math floor APIs
adinauer 074d785
changelog
adinauer bea4721
Merge branch 'main' into fix/android-floor-div
adinauer a8b6357
fix(android): Add attributed floor math helpers
adinauer b6de3dc
Merge remote-tracking branch 'origin/fix/android-floor-div' into fix/…
adinauer e9f735e
fix(android): Use independent floor math helpers
adinauer fc9996a
fix(android): Adapt floor math from Guava
adinauer b5c0278
changelog
adinauer 59beb23
docs(changelog): Mention Android NoSuchMethodError
adinauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Adapted from https://github.com/google/guava/blob/v33.0.0/guava/src/com/google/common/math/LongMath.java | ||
| * | ||
| * Copyright (C) 2011 The Guava Authors | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package io.sentry.vendor; | ||
|
|
||
| final class SentryMath { | ||
|
|
||
| private SentryMath() {} | ||
|
|
||
| static long floorDiv(final long x, final long y) { | ||
| final long quotient = x / y; | ||
| final long remainder = x - y * quotient; | ||
|
|
||
| if (remainder == 0) { | ||
| return quotient; | ||
| } | ||
|
|
||
| final int signum = 1 | (int) ((x ^ y) >> (Long.SIZE - 1)); | ||
| return signum < 0 ? quotient + signum : quotient; | ||
| } | ||
|
|
||
| static long floorMod(final long x, final long y) { | ||
| return x - floorDiv(x, y) * y; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.sentry.vendor | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFailsWith | ||
|
|
||
| class SentryMathTest { | ||
| @Test | ||
| fun `floorDiv matches Java Math floorDiv`() { | ||
| values.forEach { x -> | ||
| divisors.forEach { y -> assertThat(SentryMath.floorDiv(x, y)).isEqualTo(Math.floorDiv(x, y)) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `floorMod matches Java Math floorMod`() { | ||
| values.forEach { x -> | ||
| divisors.forEach { y -> assertThat(SentryMath.floorMod(x, y)).isEqualTo(Math.floorMod(x, y)) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `floorDiv throws when dividing by zero`() { | ||
| assertFailsWith<ArithmeticException> { SentryMath.floorDiv(1L, 0L) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `floorMod throws when dividing by zero`() { | ||
| assertFailsWith<ArithmeticException> { SentryMath.floorMod(1L, 0L) } | ||
| } | ||
|
|
||
| private companion object { | ||
| val values = | ||
| listOf( | ||
| Long.MIN_VALUE, | ||
| -86_400_001L, | ||
| -86_400_000L, | ||
| -86_399_999L, | ||
| -401L, | ||
| -400L, | ||
| -399L, | ||
| -2L, | ||
| -1L, | ||
| 0L, | ||
| 1L, | ||
| 2L, | ||
| 399L, | ||
| 400L, | ||
| 401L, | ||
| 86_399_999L, | ||
| 86_400_000L, | ||
| 86_400_001L, | ||
| Long.MAX_VALUE, | ||
| ) | ||
|
|
||
| val divisors = listOf(Long.MIN_VALUE, -146_097L, -400L, -1L, 1L, 400L, 146_097L, Long.MAX_VALUE) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.