Skip to content

Commit

Permalink
Write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolasan committed Aug 9, 2024
1 parent 44fe4dc commit c1c6b1c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class ImperialListAdapter(private val workingUnits: WorkingUnits) : BaseAdapter(
}
}
names = allUnits.map { u -> u.unitName.name.lowercase(Locale.ROOT) }
// units = units
notifyDataSetChanged()
//notifyDataSetChanged()
}

override fun getView(position: Int, contentView: View?, parent: ViewGroup?): View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ fun convertValue(inputUnit: ImperialUnit?, outputUnit: ImperialUnit?, inputValue
inputUnit ?: return 0.0
outputUnit ?: return 0.0
val formulaArray = findConversionFormula(inputUnit.category.nameMap, inputUnit, outputUnit)

if (!outputUnit.formulaMap.containsKey(inputUnit.unitName)) {
outputUnit.formulaMap[inputUnit.unitName] = formulaArray
}

val it = formulaArray.iterator()
var x = inputValue
while (it.hasNext()) {
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/res/layout-sw600dp/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
android:layout_height="0dp"
app:layout_constraintHeight_max="@dimen/max_description_height"
app:layout_constraintBottom_toTopOf="@id/converter"
app:layout_constraintEnd_toStartOf="@id/list"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/list"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
tools:layout="@layout/fragment_switch" />
Expand Down Expand Up @@ -91,7 +93,7 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/converter"
app:layout_constraintEnd_toStartOf="@id/categories"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
Expand All @@ -101,13 +103,15 @@
<fragment
android:id="@+id/converter"
android:name="xyz.neupokoev.forgottenstandards.ConverterFragment"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_min="@dimen/min_converter_height"
app:layout_constraintBottom_toTopOf="@id/keyboard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/list"
app:layout_constraintTop_toBottomOf="@id/categories"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
tools:layout="@layout/fragment_converter" />
Expand All @@ -116,12 +120,17 @@
android:id="@+id/keyboard"
android:name="xyz.neupokoev.forgottenstandards.KeyboardFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:elevation="2dp"
android:visibility="visible"
android:layout_margin="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/list"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
tools:layout="@layout/keyboard_panel"
tools:targetApi="lollipop" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package xyz.neupokoev.forgottenstandards

import io.github.mikolasan.convertmeifyoucan.FunctionParser
import io.github.mikolasan.ratiogenerator.MinLengthUnits
import io.github.mikolasan.ratiogenerator.findConversionFormula
import org.junit.Assert.assertEquals
import org.junit.Test

class ConvertValueTest {
@Test
fun allLengthUnits() {
val units = MinLengthUnits.units
val start = System.currentTimeMillis()
for (fromUnit in units) {
for (toUnit in units) {
if (fromUnit.unitName == toUnit.unitName) continue
val ret = convertValue(fromUnit, toUnit, 42.0)
assert(ret.isFinite())
}
}

for (fromUnit in units) {
for (toUnit in units) {
if (fromUnit.unitName == toUnit.unitName) continue
val ret = convertValue(fromUnit, toUnit, 42.0)
assert(ret.isFinite())
}
}
val end = System.currentTimeMillis()
println("allLengthUnits took " + (end - start) + " MilliSeconds")
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package xyz.neupokoev.forgottenstandards

import io.github.mikolasan.ratiogenerator.ImperialUnit
import io.github.mikolasan.ratiogenerator.ImperialUnitCategory
import io.github.mikolasan.ratiogenerator.ImperialUnitName
import io.github.mikolasan.ratiogenerator.ImperialUnitType
import io.github.mikolasan.ratiogenerator.MinLengthUnits
import org.junit.Assert.*
import org.junit.Test


class ImperialUnitLengthTest {
val defaultValue = ImperialUnit(ImperialUnitType.LENGTH, ImperialUnitName.LENGTH_ZERO_NO_UNIT, mutableMapOf())
val imperialUnits = LengthUnits.nameMap
val defaultValue = ImperialUnit(MinLengthUnits, ImperialUnitType.LENGTH, ImperialUnitName.LENGTH_ZERO_NO_UNIT)
val imperialUnits = MinLengthUnits.nameMap
val arshin = imperialUnits.getOrDefault(ImperialUnitName.ARSHIN, defaultValue)
val point = imperialUnits.getOrDefault(ImperialUnitName.POINT, defaultValue)
val line = imperialUnits.getOrDefault(ImperialUnitName.LINE, defaultValue)
Expand Down Expand Up @@ -160,7 +162,7 @@ class ImperialUnitLengthTest {

@Test
fun getConversionRatio_checkRatioForAllUnits() {
val units = LengthUnits.units
val units = MinLengthUnits.units
for (fromUnit in units) {
for (toUnit in units) {
println("Finding ratio for ${fromUnit.unitName} -> ${toUnit.unitName}...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ImperialUnit(val category: ImperialUnitCategory,
var bookmarked = false

var ratioMap: Map<ImperialUnitName, Double> = mapOf()
var formulaMap: Map<ImperialUnitName, Array<String>> = mapOf()
var formulaMap: MutableMap<ImperialUnitName, Array<String>> = mutableMapOf()

fun restoreValue(s: String, v: Double) {
inputString = s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ abstract class ImperialUnitCategory(val type: ImperialUnitType,
.filter { it.second.second == unit.unitName }
.map { it.first.second to (it.second.first / it.first.first) }
unit.ratioMap = (leftToRight + rightToLeft).toMap()
unit.formulaMap = unit.ratioMap.map { it.key to arrayOf("x * ${it.value}") }.toMap()
unit.formulaMap = unit.ratioMap.map { it.key to arrayOf("x * ${it.value}") }.toMap(mutableMapOf())
}
} else if (formulaList.isNotEmpty()) {
// fill formula maps
units.forEach { unit ->
unit.formulaMap = formulaList
.filter { it.first == unit.unitName }
.associate { it.third to arrayOf(it.second) }
.toMutableMap()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ class CompleteConversionTest {
fun allLengthUnits() {
val units = MinLengthUnits.units
val map = MinLengthUnits.nameMap
val start = System.currentTimeMillis()
for (fromUnit in units) {
for (toUnit in units) {
if (fromUnit.unitName == toUnit.unitName) continue

println("Finding ratio for ${fromUnit.unitName} -> ${toUnit.unitName}...")
//println("Finding ratio for ${fromUnit.unitName} -> ${toUnit.unitName}...")
val formulaArray = findConversionFormula(map, fromUnit, toUnit)
assert(formulaArray.isNotEmpty())
println("[OK]: ${fromUnit.unitName} -> ${formulaArray.contentToString()} = ${toUnit.unitName}")
//println("[OK]: ${fromUnit.unitName} -> ${formulaArray.contentToString()} = ${toUnit.unitName}")
}
}
val end = System.currentTimeMillis()
println("allLengthUnits took " + (end - start) + " MilliSeconds")
}

@Test
Expand Down

0 comments on commit c1c6b1c

Please sign in to comment.