From 732ccd6d7d041c05d2d5a72603bbbf0d7e77aa42 Mon Sep 17 00:00:00 2001
From: Marius Renner <marius.renner@mni.thm.de>
Date: Tue, 26 Dec 2017 15:37:13 +0100
Subject: [PATCH] Fix rounding error in ScoreBasedScoreCalculator

Cast int to double before division to avoid truncation of the division
result. Changed corresponding test so that it fails with the old
implementation.

Cherry-picked and backported from GH-52.
---
 .../domain/PointBasedLearningProgress.java    |  2 +-
 .../PointBasedLearningProgressTest.java       | 29 ++++++++++---------
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java b/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java
index 7868b343e..b34c9f83a 100644
--- a/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java
+++ b/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java
@@ -48,7 +48,7 @@ public class PointBasedLearningProgress extends VariantLearningProgress {
 		if (courseMaximumValue == 0 || numUsers == 0) {
 			return 0;
 		}
-		final double courseAverageValue = userTotalValue / numUsers;
+		final double courseAverageValue = (double) userTotalValue / numUsers;
 		final double courseProgress = courseAverageValue / courseMaximumValue;
 		return (int) Math.min(100, Math.round(courseProgress * 100));
 	}
diff --git a/src/test/java/de/thm/arsnova/domain/PointBasedLearningProgressTest.java b/src/test/java/de/thm/arsnova/domain/PointBasedLearningProgressTest.java
index 9e8e6ba76..f6a71ef17 100644
--- a/src/test/java/de/thm/arsnova/domain/PointBasedLearningProgressTest.java
+++ b/src/test/java/de/thm/arsnova/domain/PointBasedLearningProgressTest.java
@@ -57,27 +57,30 @@ public class PointBasedLearningProgressTest {
 
 	@Test
 	public void shouldFilterBasedOnQuestionVariant() {
+		// Total of 300 Points
 		String q1 = this.addQuestion("lecture", 100);
-		String q2 = this.addQuestion("preparation", 100);
+		String q2 = this.addQuestion("lecture", 100);
+		String q3 = this.addQuestion("lecture", 100);
 		User u1 = new TestUser("user1");
 		User u2 = new TestUser("user2");
-		// first question is answered correctly, second one is not
+		User u3 = new TestUser("user3");
+		// Both users achieve 200 points
 		this.addAnswer(q1, u1, 100);
 		this.addAnswer(q1, u2, 100);
+		this.addAnswer(q1, u3, 0);
 		this.addAnswer(q2, u1, 0);
-		this.addAnswer(q2, u2, 0);
+		this.addAnswer(q2, u2, 100);
+		this.addAnswer(q2, u3, 0);
+		this.addAnswer(q3, u1, 100);
+		this.addAnswer(q3, u2, 100);
+		this.addAnswer(q3, u3, 0);
 
 		lp.setQuestionVariant("lecture");
-		LearningProgressValues lectureProgress = lp.getCourseProgress(null);
-		LearningProgressValues myLectureProgress = lp.getMyProgress(null, u1);
-		lp.setQuestionVariant("preparation");
-		LearningProgressValues prepProgress = lp.getCourseProgress(null);
-		LearningProgressValues myPrepProgress = lp.getMyProgress(null, u1);
-
-		assertEquals(100, lectureProgress.getCourseProgress());
-		assertEquals(100, myLectureProgress.getMyProgress());
-		assertEquals(0, prepProgress.getCourseProgress());
-		assertEquals(0, myPrepProgress.getMyProgress());
+		LearningProgressValues u1LectureProgress = lp.getMyProgress(null, u1);
+		// (500/3) / 300 ~= 0,56.
+		assertEquals(56, u1LectureProgress.getCourseProgress());
+		// 200 / 300 ~= 0,67.
+		assertEquals(67, u1LectureProgress.getMyProgress());
 	}
 
 	@Test
-- 
GitLab