Files
plot/tests/plot_widget_behavior.zi
T

180 lines
6.6 KiB
Ziran

#import "kryon/session"
test_session: Session;
TestSession :: () -> Session {
if !SessionValid(test_session) { test_session = SessionOpen() }
return test_session
}
#import "kryon/geometry"
#import "kryon/drawing_props"
#import "kryon/paint_queue"
#import "plot"
#import "plot_props"
#import "plot_widget"
#import "band_chart"
#import "band_chart_props"
#import "kryon/tree"
#import "kryon/widget_kind"
using PlotMode;
using WidgetKind;
phase: s32;
#program_export
Frame :: () -> s32 {
lines: [3]float32
lines[0] = 0.0
lines[1] = 0.5
lines[2] = 1.0
bars: [3]float32
bars[0] = 0.25
bars[1] = 0.5
bars[2] = 1.0
automatic: PlotProps
observed: PlotRange = PlotObservedRange(bars[0:3], -1,
automatic)
if observed.min_value != 0.25 ||
observed.max_value != 1.0 ||
observed.range != 0.75 { return -2 }
if PlotOffset(0, 5) != 0 || PlotOffset(4, 6) != 2 ||
PlotOffset(4, -1) != 3 || PlotOffset(4, -6) != 2 ||
PlotOffset(1, 99) != 0 { return -3 }
fixed: PlotRange = PlotRangeFor(0.0, 10.0, -1.0, 20.0)
if fixed.min_value != 0.0 || fixed.max_value != 10.0 ||
fixed.range != 10.0 || PlotNormalize(5.0, fixed) != 0.5 ||
PlotNormalize(-5.0, fixed) != 0.0 ||
PlotNormalize(50.0, fixed) != 1.0 { return -4 }
singleton: PlotRange = PlotRangeFor(10.0, 5.0, 3.0, 3.0)
if singleton.min_value != 2.5 ||
singleton.max_value != 3.5 ||
singleton.range != 1.0 { return -5 }
line_props: PlotProps
line_props.key = cast(u64)11
line_props.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
line_props.label = "Trend"
line_props.semantic_label = "XMR price trend"
line_props.overlay = "Now"
line_props.scale_min = 0.0
line_props.scale_max = 1.0
bar_props: PlotProps
bar_props.key = cast(u64)12
bar_props.bounds = Rectangle.{10.0, 90.0, 100.0, 60.0}
bar_props.label = "Bars"
bar_props.overlay = "Peak"
bar_props.offset = 1
bar_props.scale_min = 0.0
bar_props.scale_max = 1.0
bar_props.mode = cast(PlotMode)PlotBars
TreeStart(TestSession(), cast(u64)1, Rectangle.{0.0, 0.0, 140.0, 180.0})
if Plot(TestSession(), line_props, lines[0:3]) != 1 ||
Plot(TestSession(), bar_props, bars[0:3]) != 2 ||
!TreeFinish(TestSession()) || TreeCount(TestSession()) != 3 ||
TreeNodeAt(TestSession(), 1).kind != WidgetKindPlot ||
TreeNodeAt(TestSession(), 2).kind != WidgetKindPlot ||
TreeNodeAt(TestSession(), 1).semantic_label != "XMR price trend" ||
TreeNodeAt(TestSession(), 2).semantic_label != "Bars" { return -1 }
PaintFlush(TestSession())
old: s32 = phase
phase += 1
return old
}
#import "plot_widget_host"
#import "line_chart"
// This week over last week: five grid lines, then two segments drawn twice
// for each series.
CompareLines :: () -> s32 {
current: [3]float32 = .[1.0, 2.0, 3.0]
previous: [3]float32 = .[4.0, 2.0, 2.0]
both: ChartRange = ChartRangeForBoth(current[:], previous[:])
if both.low != 1.0 || both.high != 4.0 { return 3 }
chart: LineChartProps
chart.key = cast(u64)21
chart.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
chart.semantic_label = "Visits this week and last week"
before: s32 = PlotLineCount()
TreeStart(TestSession(), cast(u64)2, Rectangle.{0.0, 0.0, 140.0, 180.0})
LineChartCompare(TestSession(), chart, current[:], previous[:], both)
if !TreeFinish(TestSession()) { return 4 }
PaintFlush(TestSession())
if PlotLineCount() - before != 5 + 4 + 4 { return 5 }
return 0
}
// The band chart's scale, slots and the point a pointer shows.
BandChecks :: () -> s32 {
props: BandChartProps
props.bounds = Rectangle.{0.0, 0.0, 312.0, 140.0}
props.slots = 30
props.min_value = 0.0
props.max_value = 10.0
props.top_label = "10"
props.bottom_label = "0"
props.font = 14
plot: Rectangle = BandChartPlotBounds(props)
if plot.x != 28.0 || plot.y != 10.0 || plot.width != 276.0 ||
plot.height != 120.0 { return 10 }
if BandChartY(plot, props, 10.0) != 10.0 || BandChartY(plot, props, 0.0) != 130.0 ||
BandChartY(plot, props, 5.0) != 70.0 || BandChartY(plot, props, 20.0) != 10.0 {
return 11
}
first: float32 = BandChartSlotX(plot, props, 0)
if first < 32.5 || first > 32.7 { return 12 }
points: [3]BandChartPoint
points[0] = BandChartPoint.{.slot = 0, .value = 2.0, .good = 1}
points[1] = BandChartPoint.{.slot = 10, .value = 5.0, .good = 0}
points[2] = BandChartPoint.{.slot = 29, .value = 9.0, .good = -1}
if BandChartNearest(props, points[:], BandChartSlotX(plot, props, 10) + 2.0, 20.0) != 1 ||
BandChartNearest(props, points[:], 300.0, 120.0) != 2 ||
BandChartNearest(props, points[0:0], 50.0, 50.0) != -1 { return 13 }
return 0
}
#program_export
main :: () -> s32 {
if ThemeChecks() != 0 { return 20 }
band := BandChecks()
if band != 0 { return band }
index: s32 = 0
while index < 2 {
if Frame() != index || PlotHostFailed() { return 1 }
if PlotBackgrounds() != (index + 1) * 2 ||
PlotMarks() != (index + 1) * 3 ||
PlotOutlines() != (index + 1) * 2 ||
PlotLineCount() != (index + 1) * 2 ||
PlotLabels() != (index + 1) * 4 { return 2 }
index += 1
}
return CompareLines()
}
ThemeToken :: (name: string, color: u32) -> bool {
bytes: [16]u8
for index: 0..cast(s32)name.count - 1 { bytes[index] = name[index] }
return SetStyleTokenColorBytes(bytes[0:name.count], color)
}
ThemeChecks :: () -> s32 {
saved := CaptureStyleTokenColors()
defer { unused RestoreStyleTokenColors(saved) }
ClearStyleTokenColors()
fallback := DefaultPlotFaces()
if fallback.plot.value.background != cast(u32)0xf5f5f5ff { return 1 }
unused ThemeToken("surface", cast(u32)0xe8dccaff)
unused ThemeToken("text", cast(u32)0x302820ff)
unused ThemeToken("border", cast(u32)0xd0b898ff)
unused ThemeToken("accent", cast(u32)0x704020ff)
light := PlotFacesFor(PlotProps.{}, DefaultPlotFaces())
if light.plot.value.background != cast(u32)0xe8dccaff ||
light.plot.value.foreground != cast(u32)0x302820ff ||
light.plot.value.border != cast(u32)0xd0b898ff ||
light.mark.value.background != cast(u32)0x704020ff { return 2 }
unused ThemeToken("surface", cast(u32)0x17212bff)
unused ThemeToken("text", cast(u32)0xf1f5f9ff)
dark := PlotFacesFor(PlotProps.{}, DefaultPlotFaces())
if dark.plot.value.background != cast(u32)0x17212bff ||
dark.plot.value.foreground != cast(u32)0xf1f5f9ff { return 3 }
return 0
}