From c8380be8dba40de4378d9536f232c4e94c9f4d03 Mon Sep 17 00:00:00 2001 From: Julian Gegner Date: Tue, 8 Nov 2022 21:37:47 +0100 Subject: [PATCH] better tests --- lib/widgets/EnergyDisplay.dart | 4 +- test/EnergyBilanceModelTest.dart | 130 ++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 5 deletions(-) diff --git a/lib/widgets/EnergyDisplay.dart b/lib/widgets/EnergyDisplay.dart index 2d0d7a2..3d1287d 100644 --- a/lib/widgets/EnergyDisplay.dart +++ b/lib/widgets/EnergyDisplay.dart @@ -35,8 +35,8 @@ class EnergyDisplay extends StatelessWidget { ), Consumer( builder: (context, model, child){ - return TabDisplayWidget('Generierte potentielle Energie: ${model.potentialEnergy}\n' - 'Generierte kinetische Energie: ${model.kineticEnergy}'); + return TabDisplayWidget('Generierte potentielle Energie: ${model.potentialEnergy} Joule\n' + 'Generierte kinetische Energie: ${model.kineticEnergy} Joule'); } ), ] diff --git a/test/EnergyBilanceModelTest.dart b/test/EnergyBilanceModelTest.dart index b43ff78..68cd9ec 100644 --- a/test/EnergyBilanceModelTest.dart +++ b/test/EnergyBilanceModelTest.dart @@ -1,13 +1,137 @@ +import 'dart:math'; + import 'package:energy_bilance/models/EnergyBilanceModel.dart'; import 'package:flutter_test/flutter_test.dart'; -void main() async { +void main() { EnergyBilanceModel model = EnergyBilanceModel(); + const double startHeight = 10; + const double endHeight = 20; + + const double startVelocity = 0; + const double endVelocity = 100; + + const double weight = 50; + + const double gravity = 9.81; + + const double waterHeatCapacity = 4.181; //unit: J / (g * C°) + const double ironHeatCapacity = 0.439; //unit: J / (g * C°) + + resetModel() { + model.setWeight(0); + model.setStartVelocity(0); + model.setEndVelocity(0); + model.setStartHeight(0); + model.setEndHeight(0); + } + group("Testing EnergyBilanceModel", () { - test("blalbb", () async { - model.setWeight(5.0); + test("initial calculations", () { expect(0, model.potentialEnergy); + expect(0, model.kineticEnergy); + expect(0, model.deltaVelocity); + expect(0, model.deltaHeight); + + expect(0, model.amountOfGlowingIronInKg); + expect(0, model.amountOfBoilingWaterInL); + + resetModel(); + }); + test("deltaHeight", () { + model.setStartHeight(startHeight); + model.setEndHeight(endHeight); + expect(endHeight - startHeight, model.deltaHeight); + + resetModel(); + }); + test("deltaVelocity", () { + model.setStartVelocity(startVelocity); + model.setEndVelocity(endVelocity); + expect(endVelocity - startVelocity, model.deltaVelocity); + + resetModel(); + }); + test("potentialEnergy", () { + model.setStartHeight(startHeight); + model.setEndHeight(endHeight); + + //without weight + expect(0, model.potentialEnergy); + + model.setWeight(weight); + expect(weight * gravity * model.deltaHeight, model.potentialEnergy); + + //kinetic energy should still be zero + expect(0, model.kineticEnergy); + resetModel(); + }); + test("kineticEnergy", () { + resetModel(); + model.setStartVelocity(startVelocity); + model.setEndVelocity(endVelocity); + + //without weight + expect(0, model.kineticEnergy); + + model.setWeight(weight); + expect(weight * pow(model.deltaVelocity, 2) / 2, model.kineticEnergy); + + //kinetic energy should still be zero + expect(0, model.potentialEnergy); + resetModel(); + }); + test("amountOfBoilingWaterInL", () { + + model.setWeight(weight); + model.setStartVelocity(startVelocity); + model.setEndVelocity(endVelocity); + + double shouldAmountWithKineticEnergy = (model.potentialEnergy + model.kineticEnergy) / waterHeatCapacity / 80 / 1000; + expect(shouldAmountWithKineticEnergy, model.amountOfBoilingWaterInL); + resetModel(); + + model.setWeight(weight); + model.setStartHeight(startHeight); + model.setEndHeight(endHeight); + + double shouldAmountWithPotentialEnergy = (model.potentialEnergy + model.kineticEnergy) / waterHeatCapacity / 80 / 1000; + expect(shouldAmountWithPotentialEnergy, model.amountOfBoilingWaterInL); + + model.setStartVelocity(startVelocity); + model.setEndVelocity(endVelocity); + + double shouldAmountWithBoth = (model.potentialEnergy + model.kineticEnergy) / waterHeatCapacity / 80 / 1000; + expect(shouldAmountWithBoth, model.amountOfBoilingWaterInL); + + resetModel(); + + }); + test("amountOfGlowingIronInKg", () { + + model.setWeight(weight); + model.setStartVelocity(startVelocity); + model.setEndVelocity(endVelocity); + + double shouldAmountWithKineticEnergy = (model.potentialEnergy + model.kineticEnergy) / ironHeatCapacity / 680 / 1000; + expect(shouldAmountWithKineticEnergy, model.amountOfGlowingIronInKg); + resetModel(); + + model.setWeight(weight); + model.setStartHeight(startHeight); + model.setEndHeight(endHeight); + + double shouldAmountWithPotentialEnergy = (model.potentialEnergy + model.kineticEnergy) / ironHeatCapacity / 680 / 1000; + expect(shouldAmountWithPotentialEnergy, model.amountOfGlowingIronInKg); + + model.setStartVelocity(startVelocity); + model.setEndVelocity(endVelocity); + + double shouldAmountWithBoth = (model.potentialEnergy + model.kineticEnergy) / ironHeatCapacity / 680 / 1000; + expect(shouldAmountWithBoth, model.amountOfGlowingIronInKg); + + resetModel(); }); }); }