From ab7ab9e52ab87963831f90ab25957d883878654d Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 2 Jun 2016 21:00:37 +0200 Subject: New ImageCache class for reusing Image resources. --- test/Makefile.am | 16 ++++++++- test/imagecachetest.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 test/imagecachetest.cc (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index 594aa0d..1b4470d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -5,7 +5,7 @@ include $(top_srcdir)/src/Makefile.am.drumgizmo TESTS = resource engine gui resampler lv2 configfile audiocache \ audiocachefile audiocacheidmanager audiocacheeventhandler \ - memchecker randomtest atomictest syncedsettingstest + memchecker randomtest atomictest syncedsettingstest imagecachetest check_PROGRAMS = $(TESTS) @@ -150,3 +150,17 @@ syncedsettingstest_SOURCES = syncedsettings.cc test.cc EXTRA_DIST = \ lv2_test_host.h + +imagecachetest_CXXFLAGS = -DOUTPUT=\"imagecachetest\" $(CPPUNIT_CFLAGS) \ + -I$(top_srcdir)/src -I$(top_srcdir)/plugingui -I$(top_srcdir)/hugin +imagecachetest_LDFLAGS = $(CPPUNIT_LIBS) +imagecachetest_SOURCES = \ + $(top_srcdir)/hugin/hugin.c \ + $(top_srcdir)/plugingui/lodepng/lodepng.cpp \ + $(top_srcdir)/plugingui/resource_data.cc \ + $(top_srcdir)/plugingui/resource.cc \ + $(top_srcdir)/plugingui/image.cc \ + $(top_srcdir)/plugingui/imagecache.cc \ + $(top_srcdir)/plugingui/colour.cc \ + imagecachetest.cc \ + test.cc diff --git a/test/imagecachetest.cc b/test/imagecachetest.cc new file mode 100644 index 0000000..2a5b6ed --- /dev/null +++ b/test/imagecachetest.cc @@ -0,0 +1,98 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * imagecachetest.cc + * + * Thu Jun 2 18:54:31 CEST 2016 + * Copyright 2016 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include + +#include + +#include +#include + +class TestableImageCache + : public GUI::ImageCache +{ +public: + std::size_t count(const std::string& filename) + { + if(imageCache.find(filename) == imageCache.end()) + { + return 0; + } + + auto it = imageCache.find(filename); + auto& val = it->second; + return val.first; + } +}; + +class ImageCacheTest + : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(ImageCacheTest); + CPPUNIT_TEST(refCountTest); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp() {} + void tearDown() {} + + void refCountTest() + { + TestableImageCache imageCache; + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 0u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 0u); + + { + auto image1{imageCache.getImage("foo")}; + (void)image1; + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 1u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 0u); + + auto image2 = imageCache.getImage("bar"); + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 1u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u); + + auto image3 = imageCache.getImage("foo"); + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 2u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u); + + { + auto image4 = imageCache.getImage("foo"); + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 3u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u); + } + + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 2u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u); + } + + CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 0u); + CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 0u); + } +}; + +// Registers the fixture into the 'registry' +CPPUNIT_TEST_SUITE_REGISTRATION(ImageCacheTest); -- cgit v1.2.3