Skip to content

Commit

Permalink
Fixes oppia#4100 : Added tests for PolynomialSubject (oppia#5667)
Browse files Browse the repository at this point in the history
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.
![Screenshot from 2025-01-26
16-03-01](https://github.com/user-attachments/assets/e194d152-e4fc-4840-a30d-fa60f739ff81)



## 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
theayushyadav11 and theayushyadav11 authored Feb 11, 2025
1 parent 404618d commit c2c81bd
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 6 deletions.
4 changes: 0 additions & 4 deletions scripts/assets/test_file_exemptions.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -4106,10 +4106,6 @@ test_file_exemption {
exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/MathParsingErrorSubject.kt"
test_file_not_required: true
}
test_file_exemption {
exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt"
test_file_not_required: true
}
test_file_exemption {
exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/TokenSubject.kt"
test_file_not_required: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import org.oppia.android.util.math.getConstant
import org.oppia.android.util.math.isConstant
import org.oppia.android.util.math.toPlainText

// TODO(#4100): Add tests for this class.

/**
* Truth subject for verifying properties of [Polynomial]s.
*
Expand Down
15 changes: 15 additions & 0 deletions testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ oppia_android_test(
"//third_party:robolectric_android-all",
],
)

oppia_android_test(
name = "PolynomialSubjectTest",
srcs = ["PolynomialSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.PolynomialSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:polynomial_subject",
"//third_party:com_google_truth_truth",
"//third_party:junit_junit",
"//third_party:robolectric_android-all",
],
)
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)
}
}
}

0 comments on commit c2c81bd

Please sign in to comment.