fix float-int error in color interpolation and add some more tests

This commit is contained in:
2019-03-10 21:34:27 +01:00
parent 2b7dca8e2f
commit 9b9e3a0d73
4 changed files with 71 additions and 25 deletions

View File

@@ -6,8 +6,8 @@ android {
applicationId "de.weseng.wifiweatherstation"
minSdkVersion 26
targetSdkVersion 28
versionCode 20190309
versionName "2019.3.9"
versionCode 20190310
versionName "2019.3.10"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {

View File

@@ -15,7 +15,6 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
@@ -25,6 +24,7 @@ import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
@@ -325,12 +325,12 @@ public class MainNativeActivity extends AppCompatActivity {
HashMap<String, String> hm = dataList.get(n - 1);
float temp = Float.parseFloat(hm.get("temperature"));
String temperature = String.format("%.1f", temp);
String temperature = String.format(Locale.getDefault(), "%.1f", temp);
float humi = Float.parseFloat(hm.get("humidity"));
String humidity = String.format("%.1f", humi);
String humidity = String.format(Locale.getDefault(), "%.1f", humi);
Color green = Color.valueOf(0xff00ff00);
Color green = Color.valueOf(0x71ff5f);
Color red = Color.valueOf(0xffd1655d);
Color blue = Color.valueOf(Color.rgb(113, 157, 195));
@@ -364,17 +364,17 @@ public class MainNativeActivity extends AppCompatActivity {
}
TextView tv;
tv = findViewById(idMin1);
tv.setText(String.format("%.1f", min1));
tv.setText(String.format(Locale.getDefault(), "%.1f", min1));
tv = findViewById(idMax1);
tv.setText(String.format("%.1f", max1));
tv.setText(String.format(Locale.getDefault(), "%.1f", max1));
tv = findViewById(idDelta1);
tv.setText(String.format("%.1f", max1 - min1));
tv.setText(String.format(Locale.getDefault(), "%.1f", max1 - min1));
tv = findViewById(idMin2);
tv.setText(String.format("%.1f", min2));
tv.setText(String.format(Locale.getDefault(), "%.1f", min2));
tv = findViewById(idMax2);
tv.setText(String.format("%.1f", max2));
tv.setText(String.format(Locale.getDefault(), "%.1f", max2));
tv = findViewById(idDelta2);
tv.setText(String.format("%.1f", max2 - min2));
tv.setText(String.format(Locale.getDefault(), "%.1f", max2 - min2));
}
}
}
@@ -400,7 +400,7 @@ public class MainNativeActivity extends AppCompatActivity {
terminateTask();
}
public void runAgain() {
void runAgain() {
// Call this to request data from the server again
lock.lock();
try {
@@ -410,7 +410,7 @@ public class MainNativeActivity extends AppCompatActivity {
}
}
public void terminateTask() {
void terminateTask() {
// The task will only finish when we call this method
finished = true;
//lock.unlock();
@@ -448,9 +448,9 @@ public class MainNativeActivity extends AppCompatActivity {
int interpolate(int pBegin, int pEnd, int pStep, int pMax) {
if (pBegin < pEnd) {
return ((pEnd - pBegin) * (pStep / pMax)) + pBegin;
return (int) ((pEnd - pBegin) * ((float) pStep / pMax)) + pBegin;
} else {
return ((pBegin - pEnd) * (1 - (pStep / pMax))) + pEnd;
return (int) ((pBegin - pEnd) * (1 - ((float) pStep / pMax))) + pEnd;
}
}

View File

@@ -20,6 +20,7 @@
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="-8dp"
android:weightSum="6">
<TextView

View File

@@ -8,26 +8,71 @@ public class MainNativeActivityTest {
@Test
public void valueToColorRGB_below() {
assertEquals(new MainNativeActivity().valueToColorRGB(
assertEquals(0x719dc3, new MainNativeActivity().valueToColorRGB(
10, 17, 19, 23, 25,
0x719dc3, 0xff00ff00, 0xffd1655d),
0x719dc3);
0x719dc3, 0xff00ff00, 0xffd1655d));
}
@Test
public void valueToColorRGB_ideal() {
assertEquals(new MainNativeActivity().valueToColorRGB(
assertEquals(0xff00ff00, new MainNativeActivity().valueToColorRGB(
20, 17, 19, 23, 25,
0x719DC3, 0xff00ff00, 0xffd1655d),
0xff00ff00);
0x719DC3, 0xff00ff00, 0xffd1655d));
}
@Test
public void valueToColorRGB_above() {
assertEquals(new MainNativeActivity().valueToColorRGB(
assertEquals(0xffd1655d, new MainNativeActivity().valueToColorRGB(
30, 17, 19, 23, 25,
0x719DC3, 0xff00ff00, 0xffd1655d),
0xffd1655d);
0x719DC3, 0xff00ff00, 0xffd1655d));
}
@Test
public void interpolate_0_255_0() {
assertEquals(0, new MainNativeActivity().interpolate(
0, 255, 0, 100));
}
@Test
public void interpolate_0_255_50() {
assertEquals(127, new MainNativeActivity().interpolate(
0, 255, 50, 100));
}
@Test
public void interpolate_0_255_100() {
assertEquals(255, new MainNativeActivity().interpolate(
0, 255, 100, 100));
}
@Test
public void interpolateColor_00ff00_ff0000_0() {
MainNativeActivity a = new MainNativeActivity();
assertEquals(0xFFFFFF & 0xff00ff00,
a.interpolateColor(0xff00ff00, 0xffff0000, 0, 100));
}
@Test
public void interpolateColor_00ff00_ff0000_50() {
MainNativeActivity a = new MainNativeActivity();
assertEquals(0xFFFFFF & 0xff7f7f00,
a.interpolateColor(0xff00ff00, 0xffff0000, 50, 100));
}
@Test
public void interpolateColor_00ff00_ff0000_100() {
MainNativeActivity a = new MainNativeActivity();
assertEquals(0xFFFFFF & 0xffff0000,
a.interpolateColor(0xff00ff00, 0xffff0000, 100, 100));
}
@Test
public void valueToColorRGB_between_ideal_and_above() {
MainNativeActivity a = new MainNativeActivity();
assertEquals(a.interpolateColor(0xff00ff00, 0xffd1655d, 50, 100),
a.valueToColorRGB(
24, 17, 19, 23, 25,
0x719DC3, 0xff00ff00, 0xffd1655d));
}
}