| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 1 | // Runs against the real V library, so it needs the native build on the loader |
| 2 | // path — `flutter test` runs on the host VM, not in the app bundle: |
| 3 | // |
| 4 | // ./tool/build.sh |
| 5 | // cd example && LD_LIBRARY_PATH=../build flutter test |
| 6 | // |
| 7 | // This is a smoke test of the bridge as the app uses it, not of the widgets. |
| 8 | import 'package:flutter/material.dart'; |
| 9 | import 'package:flutter_test/flutter_test.dart'; |
| 10 | import 'package:vflutter_ffi/vflutter_ffi.dart' as v; |
| 11 | import 'package:vflutter_ffi_example/mandelbrot_dart.dart'; |
| 12 | |
| 13 | void main() { |
| 14 | _zoomTests(); |
| 15 | |
| 16 | const view = v.FractalView(width: 64, height: 48, maxIter: 80); |
| 17 | |
| 18 | test('V fills the caller-owned buffer', () { |
| 19 | final pixels = v.render(view); |
| 20 | expect(pixels.length, view.width * view.height * 4); |
| 21 | // Alpha is written for every pixel, inside the set and out. |
| 22 | for (var i = 3; i < pixels.length; i += 4) { |
| 23 | expect(pixels[i], 255); |
| 24 | } |
| 25 | // The frame is not uniform — it actually contains a fractal. |
| 26 | expect(pixels.toSet().length, greaterThan(8)); |
| 27 | }); |
| 28 | |
| 29 | test('band split matches a single-shot render', () async { |
| 30 | final whole = v.render(view); |
| 31 | final tiled = await v.renderParallel(view, tiles: 4); |
| 32 | expect(tiled, equals(whole)); |
| 33 | }); |
| 34 | |
| 35 | test('the V and Dart kernels agree pixel-for-pixel', () { |
| 36 | expect(v.render(view), equals(renderDart(view))); |
| 37 | }); |
| 38 | |
| 39 | testWidgets('renders a frame computed in V', (tester) async { |
| 40 | await tester.pumpWidget(const MaterialApp(home: SizedBox())); |
| 41 | // The explorer renders asynchronously; just assert the bridge is usable |
| 42 | // from the test binding, which is where FFI setup usually breaks. |
| 43 | expect(v.add(20, 22), 42); |
| 44 | expect(v.greet('Flutter'), 'Hello, Flutter, from V!'); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | void _zoomTests() { |
| 49 | group('zoomedAt', () { |
| 50 | const view = v.FractalView(width: 800, height: 600); |
| 51 | |
| 52 | test('keeps the anchor point fixed in the complex plane', () { |
| 53 | // The plane coordinate under a given viewport fraction must be the same |
| 54 | // before and after the zoom. This is the property that broke when the |
| 55 | // maths lived inline in the widget. |
| 56 | double planeX(v.FractalView f, double fx) => |
| 57 | f.centerX + (fx - 0.5) * f.scale * (f.width / f.height); |
| 58 | double planeY(v.FractalView f, double fy) => |
| 59 | f.centerY + (fy - 0.5) * f.scale; |
| 60 | |
| 61 | for (final p in [0.0, 0.25, 0.5, 0.73, 1.0]) { |
| 62 | final zoomed = view.zoomedAt(p, p, 0.5); |
| 63 | expect(planeX(zoomed, p), closeTo(planeX(view, p), 1e-12)); |
| 64 | expect(planeY(zoomed, p), closeTo(planeY(view, p), 1e-12)); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | test('one tap is exactly one zoom level', () { |
| 69 | expect(view.zoomedAt(0.5, 0.5, 0.5).scale, closeTo(view.scale / 2, 1e-12)); |
| 70 | expect( |
| 71 | view.zoomedAt(0.5, 0.5, 0.5).zoomedAt(0.5, 0.5, 0.5).scale, |
| 72 | closeTo(view.scale / 4, 1e-12), |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | test('zooming in then out returns to the starting view', () { |
| 77 | final round = view.zoomedAt(0.3, 0.8, 0.5).zoomedAt(0.3, 0.8, 2.0); |
| 78 | expect(round.scale, closeTo(view.scale, 1e-12)); |
| 79 | expect(round.centerX, closeTo(view.centerX, 1e-12)); |
| 80 | expect(round.centerY, closeTo(view.centerY, 1e-12)); |
| 81 | }); |
| 82 | }); |
| 83 | } |