From patchwork Mon Jan 7 23:11:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 173 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6DC2E60B3B for ; Tue, 8 Jan 2019 00:10:51 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 0C81D530 for ; Tue, 8 Jan 2019 00:10:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1546902651; bh=EFC44LTYh6j6cf8EntrQmYs5h81lgV/hX2CyS04vst4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=e7gulBaor8unftMQ1vxzhjy9GVC0qUpc1fVRKYcVhy2qvEY6tmglmPfjg7u9hS0sn MeZ/S/G79kQrs/2hN6/+CLv13Jbu+Kk6G2ICM3vXpHp4/+KP2/pPCccA+upC51G3Wx /7pW+layo+TjVvBDKEX6m7eYXQH2awqGCp91eono= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 8 Jan 2019 01:11:50 +0200 Message-Id: <20190107231151.23291-11-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> References: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 10/11] test: Add timer test X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Jan 2019 23:10:51 -0000 Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- test/meson.build | 1 + test/timer.cpp | 149 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 test/timer.cpp diff --git a/test/meson.build b/test/meson.build index 4000bd51808a..f26c8701d827 100644 --- a/test/meson.build +++ b/test/meson.build @@ -5,6 +5,7 @@ subdir('media_device') public_tests = [ ['list-cameras', 'list-cameras.cpp'], ['signal', 'signal.cpp'], + ['timer', 'timer.cpp'], ] internal_tests = [ diff --git a/test/timer.cpp b/test/timer.cpp new file mode 100644 index 000000000000..6a3cda70bef4 --- /dev/null +++ b/test/timer.cpp @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * timer.cpp - Timer test + */ + +#include + +#include +#include +#include + +#include "test.h" + +using namespace std; +using namespace libcamera; + +class ManagedTimer : public Timer +{ +public: + ManagedTimer() : Timer() + { + timeout.connect(this, &ManagedTimer::timeoutHandler); + } + + void start(int msec) + { + interval_ = msec; + clock_gettime(CLOCK_MONOTONIC, &start_); + expiration_ = { 0, 0 }; + + Timer::start(msec); + } + + int jitter() + { + int duration = (expiration_.tv_sec - start_.tv_sec) * 1000; + duration += (expiration_.tv_nsec - start_.tv_nsec) / 1000000; + return abs(duration - interval_); + } + +private: + void timeoutHandler(Timer *timer) + { + clock_gettime(CLOCK_MONOTONIC, &expiration_); + } + + int interval_; + struct timespec start_; + struct timespec expiration_; +}; + +class TimerTest : public Test +{ +protected: + int init() + { + return 0; + } + + int run() + { + EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher(); + ManagedTimer timer; + ManagedTimer timer2; + + /* Timer expiration. */ + timer.start(1000); + + if (!timer.isRunning()) { + cout << "Timer expiration test failed" << endl; + return TestFail; + } + + dispatcher->processEvents(); + + if (timer.isRunning() || timer.jitter() > 50) { + cout << "Timer expiration test failed" << endl; + return TestFail; + } + + /* Timer restart. */ + timer.start(500); + + if (!timer.isRunning()) { + cout << "Timer restart test failed" << endl; + return TestFail; + } + + dispatcher->processEvents(); + + if (timer.isRunning() || timer.jitter() > 50) { + cout << "Timer restart test failed" << endl; + return TestFail; + } + + /* Two timers. */ + timer.start(1000); + timer2.start(300); + + dispatcher->processEvents(); + + if (!timer.isRunning()) { + cout << "Two timers test failed" << endl; + return TestFail; + } + + if (timer2.jitter() > 50) { + cout << "Two timers test failed" << endl; + return TestFail; + } + + dispatcher->processEvents(); + + if (timer.jitter() > 50) { + cout << "Two timers test failed" << endl; + return TestFail; + } + + /* Restart timer before expiration. */ + timer.start(1000); + timer2.start(300); + + dispatcher->processEvents(); + + if (timer2.jitter() > 50) { + cout << "Two timers test failed" << endl; + return TestFail; + } + + timer.start(1000); + + dispatcher->processEvents(); + + if (timer.jitter() > 50) { + cout << "Two timers test failed" << endl; + return TestFail; + } + + return TestPass; + } + + void cleanup() + { + } +}; + +TEST_REGISTER(TimerTest)