forked from oppia/oppia-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix oppia#4100 ### New Tests for Polynomial Functionality: * **Build Configuration Update:** * Added a new test target `PolynomialSubjectTest` to the `BUILD.bazel` file. This includes necessary dependencies and configurations for the new test. * **New Test File:** * Created `PolynomialSubjectTest.kt` which contains various test cases to validate polynomial operations. This includes tests for checking invalid polynomials, constant polynomials, term counts, and polynomial evaluations. #### Screenshot of the passing tests.  ## Essential Checklist <!-- Please tick the relevant boxes by putting an "x" in them. --> - [x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)). This pull request introduces a new set of tests for polynomial-related functionality in the `oppia_android_test` project. The primary changes include the addition of a new test file and corresponding updates to the build configuration. --------- Co-authored-by: theayushyadav11 <your.email@example.com>
- Loading branch information
1 parent
404618d
commit c2c81bd
Showing
4 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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
173 changes: 173 additions & 0 deletions
173
testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt
This file contains 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,173 @@ | ||
package org.oppia.android.testing.math | ||
|
||
import org.junit.Assert.assertThrows | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import org.oppia.android.app.model.Polynomial | ||
import org.oppia.android.app.model.Real | ||
|
||
/** Tests for [PolynomialSubject]. */ | ||
@RunWith(JUnit4::class) | ||
class PolynomialSubjectTest { | ||
|
||
/** Helper function to create a polynomial term. */ | ||
fun createTerm(coefficient: Int, vararg variables: Pair<String, Int>): Polynomial.Term { | ||
return Polynomial.Term.newBuilder() | ||
.setCoefficient(Real.newBuilder().setInteger(coefficient)) | ||
.apply { | ||
variables.forEach { (name, power) -> | ||
addVariable(Polynomial.Term.Variable.newBuilder().setName(name).setPower(power)) | ||
} | ||
} | ||
.build() | ||
} | ||
|
||
/** Helper function to create a polynomial from multiple terms. */ | ||
fun createPolynomial(vararg terms: Polynomial.Term): Polynomial { | ||
return Polynomial.newBuilder().apply { | ||
terms.forEach { addTerm(it) } | ||
}.build() | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withNullPolynomial_isNotValidPolynomial() { | ||
PolynomialSubject.assertThat(null).isNotValidPolynomial() | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withNonNullPolynomial_isNotValidPolynomial_fails() { | ||
val polynomial = createPolynomial( | ||
createTerm(5, "x" to 1) | ||
) | ||
assertThrows(AssertionError::class.java) { | ||
PolynomialSubject.assertThat(polynomial).isNotValidPolynomial() | ||
} | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withConstantPolynomial_isConstantThat() { | ||
val constantPolynomial = createPolynomial( | ||
createTerm(5) | ||
) | ||
PolynomialSubject.assertThat(constantPolynomial) | ||
.isConstantThat() | ||
.isIntegerThat() | ||
.isEqualTo(5) | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_testIsConstantThat_withNonConstantPolynomial_fails() { | ||
val nonConstantPolynomial = createPolynomial( | ||
createTerm(5, "x" to 1) | ||
) | ||
|
||
assertThrows(AssertionError::class.java) { | ||
PolynomialSubject.assertThat(nonConstantPolynomial).isConstantThat() | ||
} | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withZeroTerms_hasTermCountThatIsEqualToZero() { | ||
val emptyPolynomial = Polynomial.newBuilder().build() | ||
PolynomialSubject.assertThat(emptyPolynomial) | ||
.hasTermCountThat() | ||
.isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withTwoTerms_hasTermCountThatIsEqualToTwo() { | ||
val multiTermPolynomial = createPolynomial( | ||
createTerm(1), | ||
createTerm(2) | ||
) | ||
|
||
PolynomialSubject.assertThat(multiTermPolynomial) | ||
.hasTermCountThat() | ||
.isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withValidIndex_termHasCoefficientAndvariable() { | ||
val polynomial = createPolynomial( | ||
createTerm(5, "x" to 2), | ||
createTerm(3, "y" to 1) | ||
) | ||
|
||
PolynomialSubject.assertThat(polynomial) | ||
.term(0) | ||
.hasCoefficientThat() | ||
.isIntegerThat() | ||
.isEqualTo(5) | ||
|
||
PolynomialSubject.assertThat(polynomial) | ||
.term(0) | ||
.variable(0) | ||
.hasNameThat() | ||
.isEqualTo("x") | ||
|
||
PolynomialSubject.assertThat(polynomial) | ||
.term(1) | ||
.hasCoefficientThat() | ||
.isIntegerThat() | ||
.isEqualTo(3) | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_failsWithInvalidIndex() { | ||
val polynomial = Polynomial.newBuilder().build() | ||
assertThrows(IndexOutOfBoundsException::class.java) { | ||
PolynomialSubject.assertThat(polynomial).term(0) | ||
} | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withConstantPolynomial_evaluatesToPlainText() { | ||
val constantPolynomial = createPolynomial( | ||
createTerm(5) | ||
) | ||
PolynomialSubject.assertThat(constantPolynomial) | ||
.evaluatesToPlainTextThat() | ||
.isEqualTo("5") | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withComplexPolynomial_evaluatesToPlainText() { | ||
val polynomial = createPolynomial( | ||
createTerm(2, "x" to 2), | ||
createTerm(3, "x" to 1), | ||
createTerm(1) | ||
) | ||
|
||
PolynomialSubject.assertThat(polynomial) | ||
.evaluatesToPlainTextThat() | ||
.isEqualTo("2x^2 + 3x + 1") | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_withTwoTerms_hasVariableCountThatIsEqualToTwo() { | ||
val polynomial = createPolynomial( | ||
createTerm(5, "x" to 2, "y" to 1) | ||
) | ||
|
||
PolynomialSubject.assertThat(polynomial) | ||
.term(0) | ||
.hasVariableCountThat() | ||
.isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun testPolynomialSubject_polynomialTermVariable_hasExpectedDetails() { | ||
val polynomial = createPolynomial( | ||
createTerm(1, "x" to 3) | ||
) | ||
|
||
PolynomialSubject.assertThat(polynomial) | ||
.term(0) | ||
.variable(0) | ||
.apply { | ||
hasNameThat().isEqualTo("x") | ||
hasPowerThat().isEqualTo(3) | ||
} | ||
} | ||
} |