Skip to content

Commit

Permalink
fix crash when max value of a serie is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
lcallarec committed Aug 27, 2020
1 parent 8946f97 commit 642deca
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/axis.vala
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,18 @@ namespace LiveChart {
float upper = LiveChart.cap((float) bounds.upper);
var divs = LiveChart.golden_divisors(upper);

float interval = upper / divs.get(0);
foreach(float div in divs) {
interval = upper / div;
if (div > 3f && div < 7f) {
break;
if (divs.size > 0) {
float interval = upper / divs.get(0);
foreach(float div in divs) {
interval = upper / div;
if (div > 3f && div < 7f) {
break;
}
}
var limit = bounds.upper == upper ? upper : bounds.upper + interval;
for (var value = 0f; value <= limit; value += interval) {
ticks.values.add(value);
}
}
var limit = bounds.upper == upper ? upper : bounds.upper + interval;
for (var value = 0f; value <= limit; value += interval) {
ticks.values.add(value);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace LiveChart {
}

public Gee.List<float?> golden_divisors(float value) {
//No golden divisors for 0
if (value == 0) {
return new Gee.ArrayList<float?>();
}

//Handle values below 1
float factor = value < 10 ? cap(100 / value) : 1f;
float working_value = value * factor;
Expand Down
9 changes: 9 additions & 0 deletions tests/axis.vala
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,13 @@ private void register_axis() {
assert(ticks.values.get(2) == 0.8f);
assert(ticks.values.get(3) == 1.2f);
});

Test.add_func("/LiveChart/YAxis#get_ticks_should_not_crash_when_max_value_is_zero", () => {

var y_axis = new LiveChart.YAxis();
y_axis.update_bounds(0);

//when //then
y_axis.get_ticks();
});
}
10 changes: 10 additions & 0 deletions tests/chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ private void register_chart() {
});
Gtk.main();
});

Test.add_func("/LiveChart/Chart/#should_not_crash_if_value_added_is_zero", () => {
//given
var chart = new LiveChart.Chart();
var serie = new LiveChart.Serie("Test");
chart.add_serie(serie);

//when //then
chart.add_value(serie, 0);
});
}
8 changes: 8 additions & 0 deletions tests/utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,13 @@ private void register_utils() {
assert(divisors.get(3) == 5f);
assert(divisors.get(4) == 2f);
assert(divisors.get(5) == 1f);
});

Test.add_func("/LiveChart/Utils/golden_divisors#ShouldNotCrashWhenComputingGoldenDivisorForZero", () => {
//given
var value = 0f;

//when //then
LiveChart.golden_divisors(value);
});
}

0 comments on commit 642deca

Please sign in to comment.