2009-04-02 12:53:39 +04:00
|
|
|
//
|
|
|
|
// Unit tests for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2022-02-19 14:55:06 +03:00
|
|
|
// Copyright 1998-2022 by Bill Spitzak and others.
|
2009-04-02 12:53:39 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
2009-04-02 12:53:39 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2009-04-02 12:53:39 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2009-04-02 12:53:39 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2009-04-02 12:53:39 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
|
2009-02-14 21:33:29 +03:00
|
|
|
// Fltk unit tests
|
|
|
|
// v0.1 - Greg combines Matthias + Ian's tests
|
|
|
|
// v0.2 - Ian's 02/12/09 fixes applied
|
|
|
|
// v0.3 - Fixes to circle desc, augmented extent tests, fixed indents, added show(argc,argv)
|
|
|
|
// v1.0 - Submit for svn
|
2009-04-12 17:48:03 +04:00
|
|
|
// v1.1 - Matthias seperated all tests into multiple source files for hopefully easier handling
|
2004-08-27 02:24:24 +04:00
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
#include "unittests.h"
|
|
|
|
|
2004-08-27 02:24:24 +04:00
|
|
|
#include <FL/Fl.H>
|
2009-04-22 11:57:35 +04:00
|
|
|
#include <FL/Fl_Double_Window.H>
|
2009-02-14 21:33:29 +03:00
|
|
|
#include <FL/Fl_Hold_Browser.H>
|
2009-04-12 17:48:03 +04:00
|
|
|
#include <FL/Fl_Help_View.H>
|
2009-02-14 21:33:29 +03:00
|
|
|
#include <FL/Fl_Group.H>
|
2004-08-27 02:24:24 +04:00
|
|
|
#include <FL/Fl_Box.H>
|
2021-03-20 23:39:28 +03:00
|
|
|
#include <FL/fl_draw.H> // fl_text_extents()
|
2022-01-16 21:02:36 +03:00
|
|
|
#include <FL/fl_string_functions.h> // fl_strdup()
|
2021-03-20 23:39:28 +03:00
|
|
|
#include <stdlib.h> // malloc, free
|
2004-08-27 02:24:24 +04:00
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
class MainWindow *mainwin = 0;
|
|
|
|
class Fl_Hold_Browser *browser = 0;
|
|
|
|
|
|
|
|
UnitTest::UnitTest(int index, const char *label, Fl_Widget* (*create)()) :
|
|
|
|
fWidget(0L)
|
|
|
|
{
|
|
|
|
fLabel = fl_strdup(label);
|
|
|
|
fCreate = create;
|
|
|
|
add(index, this);
|
|
|
|
}
|
2004-08-27 02:24:24 +04:00
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
UnitTest::~UnitTest() {
|
|
|
|
delete fWidget;
|
|
|
|
free(fLabel);
|
|
|
|
}
|
2004-08-27 02:24:24 +04:00
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
const char *UnitTest::label() {
|
|
|
|
return fLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitTest::create() {
|
|
|
|
fWidget = fCreate();
|
|
|
|
if (fWidget) fWidget->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitTest::show() {
|
|
|
|
if (fWidget) fWidget->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitTest::hide() {
|
|
|
|
if (fWidget) fWidget->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitTest::add(int index, UnitTest *t) {
|
|
|
|
fTest[index] = t;
|
|
|
|
if (index>=nTest)
|
|
|
|
nTest = index+1;
|
|
|
|
}
|
2009-02-14 21:33:29 +03:00
|
|
|
|
2009-04-12 17:48:03 +04:00
|
|
|
int UnitTest::nTest = 0;
|
2022-02-06 17:22:24 +03:00
|
|
|
UnitTest *UnitTest::fTest[200] = { 0 };
|
|
|
|
|
|
|
|
MainWindow::MainWindow(int w, int h, const char *l) :
|
|
|
|
Fl_Double_Window(w, h, l),
|
|
|
|
fTestAlignment(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void MainWindow::drawAlignmentIndicators() {
|
|
|
|
const int sze = 16;
|
|
|
|
// top left corner
|
|
|
|
fl_color(FL_GREEN); fl_yxline(0, sze, 0, sze);
|
|
|
|
fl_color(FL_RED); fl_yxline(-1, sze, -1, sze);
|
|
|
|
fl_color(FL_WHITE); fl_rectf(3, 3, sze-2, sze-2);
|
|
|
|
fl_color(FL_BLACK); fl_rect(3, 3, sze-2, sze-2);
|
|
|
|
// bottom left corner
|
|
|
|
fl_color(FL_GREEN); fl_yxline(0, h()-sze-1, h()-1, sze);
|
|
|
|
fl_color(FL_RED); fl_yxline(-1, h()-sze-1, h(), sze);
|
|
|
|
fl_color(FL_WHITE); fl_rectf(3, h()-sze-1, sze-2, sze-2);
|
|
|
|
fl_color(FL_BLACK); fl_rect(3, h()-sze-1, sze-2, sze-2);
|
|
|
|
// bottom right corner
|
|
|
|
fl_color(FL_GREEN); fl_yxline(w()-1, h()-sze-1, h()-1, w()-sze-1);
|
|
|
|
fl_color(FL_RED); fl_yxline(w(), h()-sze-1, h(), w()-sze-1);
|
|
|
|
fl_color(FL_WHITE); fl_rectf(w()-sze-1, h()-sze-1, sze-2, sze-2);
|
|
|
|
fl_color(FL_BLACK); fl_rect(w()-sze-1, h()-sze-1, sze-2, sze-2);
|
|
|
|
// top right corner
|
|
|
|
fl_color(FL_GREEN); fl_yxline(w()-1, sze, 0, w()-sze-1);
|
|
|
|
fl_color(FL_RED); fl_yxline(w(), sze, -1, w()-sze-1);
|
|
|
|
fl_color(FL_WHITE); fl_rectf(w()-sze-1, 3, sze-2, sze-2);
|
|
|
|
fl_color(FL_BLACK); fl_rect(w()-sze-1, 3, sze-2, sze-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::draw() {
|
|
|
|
Fl_Double_Window::draw();
|
|
|
|
if (fTestAlignment) {
|
|
|
|
drawAlignmentIndicators();
|
2004-08-27 04:22:28 +04:00
|
|
|
}
|
2022-02-06 17:22:24 +03:00
|
|
|
}
|
2009-02-14 21:33:29 +03:00
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
void MainWindow::testAlignment(int v) {
|
|
|
|
fTestAlignment = v;
|
|
|
|
redraw();
|
|
|
|
}
|
2004-08-27 04:22:28 +04:00
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
//------- include the various unit tests as inline code -------
|
2005-11-02 15:52:44 +03:00
|
|
|
|
2009-04-12 17:48:03 +04:00
|
|
|
// callback whenever the browser value changes
|
2009-02-14 21:33:29 +03:00
|
|
|
void Browser_CB(Fl_Widget*, void*) {
|
|
|
|
for ( int t=1; t<=browser->size(); t++ ) {
|
2009-04-12 17:48:03 +04:00
|
|
|
UnitTest *ti = (UnitTest*)browser->data(t);
|
2009-02-14 21:33:29 +03:00
|
|
|
if ( browser->selected(t) ) {
|
2009-04-12 17:48:03 +04:00
|
|
|
ti->show();
|
2009-02-14 21:33:29 +03:00
|
|
|
} else {
|
2009-04-12 17:48:03 +04:00
|
|
|
ti->hide();
|
2009-02-14 21:33:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-12 17:48:03 +04:00
|
|
|
|
|
|
|
|
2017-12-13 21:30:19 +03:00
|
|
|
// This is the main call. It creates the window and adds all previously
|
2009-04-12 17:48:03 +04:00
|
|
|
// registered tests to the browser widget.
|
2009-02-14 21:33:29 +03:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
Fl::args(argc,argv);
|
2015-02-09 21:04:31 +03:00
|
|
|
Fl::get_system_colors();
|
|
|
|
Fl::scheme(Fl::scheme()); // init scheme before instantiating tests
|
2009-04-12 17:48:03 +04:00
|
|
|
Fl::visual(FL_RGB);
|
2022-02-06 17:22:24 +03:00
|
|
|
Fl::use_high_res_GL(1);
|
2017-12-13 21:30:19 +03:00
|
|
|
mainwin = new MainWindow(MAINWIN_W, MAINWIN_H, "FLTK Unit Tests");
|
2009-02-14 21:33:29 +03:00
|
|
|
browser = new Fl_Hold_Browser(BROWSER_X, BROWSER_Y, BROWSER_W, BROWSER_H, "Unit Tests");
|
|
|
|
browser->align(FL_ALIGN_TOP|FL_ALIGN_LEFT);
|
|
|
|
browser->when(FL_WHEN_CHANGED);
|
|
|
|
browser->callback(Browser_CB);
|
2009-04-12 17:48:03 +04:00
|
|
|
|
|
|
|
int i, n = UnitTest::numTest();
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
UnitTest *t = UnitTest::test(i);
|
2022-02-06 17:22:24 +03:00
|
|
|
if (t) {
|
|
|
|
mainwin->begin();
|
|
|
|
t->create();
|
|
|
|
mainwin->end();
|
|
|
|
browser->add(t->label(), (void*)t);
|
|
|
|
}
|
2009-04-12 17:48:03 +04:00
|
|
|
}
|
|
|
|
|
2009-02-14 21:33:29 +03:00
|
|
|
mainwin->resizable(mainwin);
|
|
|
|
mainwin->show(argc,argv);
|
|
|
|
// Select first test in browser, and show that test.
|
2022-02-06 17:22:24 +03:00
|
|
|
browser->select(kTestAbout+1);
|
2009-02-14 21:33:29 +03:00
|
|
|
Browser_CB(browser,0);
|
|
|
|
return(Fl::run());
|
|
|
|
}
|