From patchwork Fri Dec 30 14:31:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9492333 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0C9D662AB9 for ; Fri, 30 Dec 2016 17:09:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E3D4A223B2 for ; Fri, 30 Dec 2016 17:09:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D648322701; Fri, 30 Dec 2016 17:09:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6912D223B2 for ; Fri, 30 Dec 2016 17:09:52 +0000 (UTC) Received: from localhost ([::1]:40833 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cN0gh-0002Ik-Gb for patchwork-qemu-devel@patchwork.kernel.org; Fri, 30 Dec 2016 12:09:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33566) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cN0bk-0007Kt-T8 for qemu-devel@nongnu.org; Fri, 30 Dec 2016 12:04:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cN0bh-0005CN-GU for qemu-devel@nongnu.org; Fri, 30 Dec 2016 12:04:44 -0500 Received: from mail-db5eur01on0117.outbound.protection.outlook.com ([104.47.2.117]:65408 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cN0bh-0005BO-3Z; Fri, 30 Dec 2016 12:04:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=EgSrYbMl99GAiC3FR0R76IIcfEXfxaEiR/8WZ0pqQwc=; b=Pz0n+3SKigfqqnbKD7o7ZLbQbGIdSmW3jUYluuMAgEZwoISGDY/E/3X7EaneG7QmS9enrxZ3fwPV9Ndz0YpAjA3J1fNc6gbiUtqfdkoZgsqarNozIcVocE/8ZsMaJzfKcH83yDC+Am6ur9Qwze32VB9qErt6RiDK+xphGTpywmE= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.6) by HE1PR0802MB2555.eurprd08.prod.outlook.com (10.175.35.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.803.11; Fri, 30 Dec 2016 14:31:59 +0000 From: Pavel Butsykin To: , Date: Fri, 30 Dec 2016 17:31:28 +0300 Message-ID: <20161230143142.18214-5-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20161230143142.18214-1-pbutsykin@virtuozzo.com> References: <20161230143142.18214-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1PR0802CA0010.eurprd08.prod.outlook.com (10.172.123.148) To HE1PR0802MB2555.eurprd08.prod.outlook.com (10.175.35.148) X-MS-Office365-Filtering-Correlation-Id: 1c98bff6-d10b-483d-5428-08d430c09d8e X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:HE1PR0802MB2555; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2555; 3:bIyRXBnXXn8/i5oeFSaG77iAyrmyLk8PoiMs9JiEmx6HHDloYrTT0aJ1ss8xBetle9jaDPsFbOueTJq3V9+gbILHgrvkcSAlBioM20uqVQolYEEZegg+twt07VTz4ZrsebAmQPtsYXZcqbY961yNG/M5U2577YRl2ExKsRJEOgzRWndb+wQK6IYXA/++oEPHUW7hag1jRzW9d+355O56cWgssj1U+BSeMISL1i9mAn4UskBeew6/oGdvSWEYXDEMR+UztCcxQxmfWv81JeLa+A== X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2555; 25:D9mC/eYw+ooOnYAYIXKOEJVSG+NZWiDipohGA4NSHVKQVZ4Ud/lNZIwPkVx1jA47v17ccN0fbu+o//O2ZQfgwSuOYGHIY0P5ycVX4HlxmGiMsBzcKa0RQPziGcTUu6YVV0KnuCydHfrnM1Mvu8gEcZhqpBBEP13ZOp4/BkeMYALC4+oq3omwhAH+GmVP/18lmg6dIJi8q4DgZjURkLMX0Z+Gog/dNnS/2z9TXEsVNV9ro+w2Q+ojJRaX9goG3Ymu1dmjOuKYz4SXW31dPb0Ag1y6nRFbR/xZKglHunAKLbtdXWaQAciUy0EDvJSivPkRqfVXM5+/G1nd0tm+ZjbRDLwQpe5302pxchjXgE1I3In2CG9XheqkrPXlT1xEhXg6ka8Sx1Ruwlv3s12bxMdUgLl6WCio6Y1yyS9rtIaBlpqAI0n25aG/5YggbRVPrmWCip/abADo+u4FqAfnPhnrllzquS6SU6DFZ4hI0D6Q/wZMZqCAN/CzVQzr+JmzkLA82RkExo/IdBoahX1TQdNY7+AaZ1TU0ih3Poyf7bbkqQEeTG1Y9/2DSIwtazdO890SsptC/eAhF0MLCYfc47m6sShmIv3xgR76ctTK2EEdWTIiw9lVeapgrGAyG6HyFZ6Ff2UOCqQBQd/IYBDFRBBgW81eKJuVqZrWSZrt2AugiqIiZqtQwbCfDzh4UN6fi7timbTvSIypc4lzvILvXKVB4coq9VSug7UyDTEmPi4US4lud2AZ/Tj08uZKOrZfrACT X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2555; 31:mw45eZP6f69YLn+ZxJ6oLMnD2dhrHuagp+RDWn/slAM4RkrW7FU/c+tACk9EfC+t8m7L/iHJDfyl8Fl/H5aFNd3wJ+Sv0LpT2vvZd+3zbqNAU0TNQV+f7XOXCgjwcqC/b+eY2l+I3B3iBFpn+tMTyLCfrrPopFydg6sfhllQtA3ogyxjfWLNhaUNdFh9N5NpAhqBDCYel6fpgf1xiHVd2qGZq81ktS+OB/9hIrbncTXapPXKbxtL+2nN/jzcIqyG; 20:CAiqm4l+wLXQXvCki+jEmj+GJB0aeO0qcveyhusSZnXUaM6hT22EVpaFqgBrubcZt6hqROrvQwkKBteaifDyvs6ry+SRla9P+bX0d0DEntyJhGGm5jVxU1d/8PY1wTdEsnNB3QtkS/9DleWvxQZHFFq0SFgLRamQAonuru1i6rGxoCX1979gYHONidwpq6p51jrNjdps5MGJRvuFFEsSjIqOsbupYEmMXdoXdkAMEqYyvaUTA9kZRzF3OUE9AooO X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(17755550239193); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6041248)(20161123564025)(20161123562025)(20161123555025)(20161123558021)(20161123560025)(6072148); SRVR:HE1PR0802MB2555; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0802MB2555; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2555; 4:/Nogkzy5jiXfsbbWG0+IJWUNJ7lbGRUuGgObwm2c3EIJfSEcG58wMypOByQ2vLjcvD7sfp/ecpRly41JL6sW0Yip0VAjMCjireVZH0WCTtTtkQjBqz+KhGC5aYbKEnZ89KoxHnZ1VnI2vBFRFydQrtQ2ztPqfgcnvgynAZdRd4WxssCLNMmO8UWLdz4smgaAFTAIZsYZjETDvqk40m3SH3VmIN9uRQLSDSFEUGRtK11eXKDctxEk2EweG7Z7p1vEcXuyBNI/wUR46LnepcCF55crK2HHKLXZ0tkYE9HvsSYVMfMq9WOy02C/jit5swYmdPxxCktGw/IpadGlLRwxQ8NOCFali+r9a5yuG64MrD7A7YHuPxRGzhY/p7mHpJ1Dtp4PoWvtqte3W4WxxWwwazqs6h0GrITUMMj5qgnjXFKVz/AZll+bNm4LjxKKlcNiSUxzov9fm+JbxXa+A1cXkWPJOFTbz13HF5XUBtMRXAufePA/MnrJypn4aQ2evxUmPWmaQMxZ90j8+CU62LXMUymHUR/cxWmRZ2qabjxdKfwCwzMgjnW+NxapjKDDTB26fBHd2/rwcRne983l00DnDvimfWi5YYKkOuN5rUxKzwNZVQqi2q/Beshi1y960ySAkSFxP/u4nbk9JYV0nz3Pbw== X-Forefront-PRVS: 0172F0EF77 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(7916002)(39450400003)(199003)(189002)(6666003)(5660300001)(189998001)(5001770100001)(50226002)(81156014)(81166006)(5003940100001)(8676002)(97736004)(47776003)(66066001)(6486002)(36756003)(6116002)(3846002)(76176999)(105586002)(33646002)(92566002)(305945005)(38730400001)(50466002)(106356001)(50986999)(1076002)(6506006)(6512006)(69596002)(4326007)(2950100002)(25786008)(2906002)(7736002)(68736007)(53416004)(42186005)(48376002)(101416001)(86362001); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2555; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0802MB2555; 23:7jpDScYDSkEbiDwh5rSMrirKYzMTnWaetbIslIC?= =?us-ascii?Q?y4kTqHTN1LZAS9Rx4AwxUPggLi1tCYpjZf3AXbSGtTTwI++iFWkR047T5V3p?= =?us-ascii?Q?wiQpEdW6BgP0U/Xf4jBhESiATi/sMw0JPWvgf5gSwiaDVVuckR3aSt4Gq3iQ?= =?us-ascii?Q?dBiMOjlKmVagbGw3XpeP9SW1Fl54KhmC2yA0gZ8e1RcEICdnJrhgLepCVLyo?= =?us-ascii?Q?BULXAd+kF2yHINByobh2Ki+eQ8JbIpyLRvhise8qLnPdSeljdVMYRm+xgbw9?= =?us-ascii?Q?fXQwprhdSdmr7RNIBzVha6OMWK6raB2ZEETf7y4J6LT0v3ZkYSEtWuQ/JKiH?= =?us-ascii?Q?z1yeyRGJ0mY7xxGu0E4YQY3brURn0HZvYyLuEHZnjLeVnkrAHMV3lQMSw1F3?= =?us-ascii?Q?/SgxTdUKcVmu5+Ja6H44EnFu8/fBYarwPQ3tvyYjlRx3gQcNeNLTCA3Ckrxv?= =?us-ascii?Q?wVO98snjx88uWZnTHkmSJIKJ7vpcQxvYYeY6GTJrkJzxWN2jSBbZO1cnxq1u?= =?us-ascii?Q?Npc5ZLxyl+QAHAEhZVn3DrXzSjPRB674g2fVj0e82zlBjPt7KZxVPpZt4Pm6?= =?us-ascii?Q?6sKjR6uRKKD8t0vFWcBVtTmkgp/tUielF9qp58Mc+qUVTaEr6b8ryXXJMDXh?= =?us-ascii?Q?JLzTqswCQfEvNGgsAXCvBtI1jWYbS+piAOCZY4bDfZwv0LlhMZCkNSmb9fAs?= =?us-ascii?Q?zmaqHJlXYvmpWtf8km43KffTeYlrGNs1qvF7b9cRav36s4Yh0wOHUMKUZlms?= =?us-ascii?Q?lnSueUK0I2q8Efnuat6wzGaKUASaO02Uh83gWlJBxeiEKRLRg8T3nLbRV6hJ?= =?us-ascii?Q?zOPVnYROGBe+ZlaXy7TN+QsVZmH8zaTwtKviuWvea/aSRoNP78K/ITJdVc1X?= =?us-ascii?Q?5XkT3P4/PyZEbAnNk7+HeAlXhyO2bPECSwOoyinrelYSKvQKWXEqYxVloFkJ?= =?us-ascii?Q?So9RB1N8aiKzmhZV069sGY19+oZH1NSCxVpqjZsbCQl5VCL6p2f69qsyWjiw?= =?us-ascii?Q?GlI9Zp/8xjpKXxrxGlupexVCm23Rx4r2Tm47rM1IW5u5BGGwfXFQ6LZ8oJKs?= =?us-ascii?Q?MjuTsx4a2ATxOme34OUniGE+fCWgbq+3CVgOc6jXvDSziyM4ZwGoNYss5HJn?= =?us-ascii?Q?sxvO3ZMmI7eM=3D?= X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2555; 6:nf7H2lX8iscz/prmaFfoc0WshK7qJcKo/U4VaDgITJq09ubQohs3vTOsFXue6JjjrTvapQLwrY4digNwHC7PnyIpjqNHy/31oiW6yEUCjgK602Aw23W0kegW/2pLOmQkyE6zuHA5Oxk9a5FWnWqhr3PxqVpU9SYik1eOvqJDq0WOfGaBZ5FyemmNTL7URJ5QziyBfAKCLDov0lNjuGpig6SD6CNAZAcW7JizorE+7cgsK1ShgF3tVxjz1Yz6E2SM0fhYsjtQiy0VNmUmBy+9CjPwZQ3zmO6fx1absBWH7fAF9UsHZUkunX0twVeOxx97jqRIOouQ5pbfh0UXN9vbtj75y8d3H5/KpzCDZTbrCVSGBQo4f/bxJyq0VDKGWIIdBcbbovxuJzQW7Xt6dDQRRvfhyG93MIGR1QV3U7ymCw0=; 5:VTfIEeaScysxTOqJcS1dwuqqkakl0r+UD8o36NSzmtq1EpMwMm20ieidEMMbojr2xTrVdIlsPrStEubUguvNOTAd9ZeVBmem6lkTDDCJxutrwb3JHXg/FTf0wHkJpTKeufDyg8EaXuZIw5e1ebKKAg==; 24:sGMsRj+BJA8UQsl9UwZ62pwlFQBQOyChIriUimCsvb7O3F1ckuh6n1UaY9M3kUxSVQfiV0iuGvgQxCRIY+31phRjmpcACJ7UcVoBnlCj3cA= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2555; 7:euJqvzGdL6s1ODquERZkJAPOLBOTlOXvFl0sS9HqhlyHGU5rlMVZq2TPMfeI4mTbDbxWKUMQqQ84tT/FG5QGJOP/9WAm850J0/5ui2XUuuuYCwFrnF5oldyOlX32OK1V6LCve7DT95jafHxz+vrkdVIWlL77PTuv1jy+xiulvJie/sUCZnyM8/9I3Q5Kos17ZksoOQI1q4Jn6Hn+YsS4H8u6gDGB73ME2tWXlFNuxyZaLyzPXpGHdByCfHqUgPmXAZepbOpzm6TEZLK9eYxUR9ZkJUCbWobaE8yDWmemjh/5LOSFfyVjjGxskcVGRgWtcetVurNQoUNHm1cx0PVZ+5ZX2MU+dVWwrSyipo85GFPRl+Elz6ro/bQY0c8oWjGyyKmFWfVNVaerNEi9Ldei83aqt2kKnXtwqOIsnb799DYdcxr4A5RfdtJA+T498Ps+zGd3HNqwd1qbmmwLcao9CQ==; 20:RcvsWW213eOIMW61PuKZumWdUhdiK8tyRS1kAIjyOJ5i6tbRNgY34M3RJyA7nTATeKp6EmD7hxc771VF9Dueaym3GfKPnxiNpJ8KkIcLayjnSqFQPfIiBDknw5E5QBWZaFBPDHx6Sz5nCJTmISOMIbqE7Uy85wZuVUgbBbzZ6Cw= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Dec 2016 14:31:59.8745 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2555 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.117 Subject: [Qemu-devel] [PATCH v2 04/18] tests/test-rbcache: add test cases X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, armbru@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Pavel Butsykin --- tests/Makefile.include | 3 + tests/test-rbcache.c | 431 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 434 insertions(+) create mode 100644 tests/test-rbcache.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 4841d582a1..74bdcd2aac 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -55,6 +55,8 @@ check-unit-y += tests/test-hbitmap$(EXESUF) gcov-files-test-hbitmap-y = blockjob.c check-unit-y += tests/test-blockjob$(EXESUF) check-unit-y += tests/test-blockjob-txn$(EXESUF) +gcov-files-test-rbcache-y = util/rbcache.c +check-unit-y += tests/test-rbcache$(EXESUF) check-unit-y += tests/test-x86-cpuid$(EXESUF) # all code tested by test-x86-cpuid is inside topology.h gcov-files-test-x86-cpuid-y = @@ -498,6 +500,7 @@ tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y) tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y) tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y) +tests/test-rbcache$(EXESUF): tests/test-rbcache.o $(test-util-obj-y) tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o migration/xbzrle.o page_cache.o $(test-util-obj-y) tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o diff --git a/tests/test-rbcache.c b/tests/test-rbcache.c new file mode 100644 index 0000000000..40ccd6b225 --- /dev/null +++ b/tests/test-rbcache.c @@ -0,0 +1,431 @@ +/* + * QEMU Range-Based Cache core + * + * Copyright (C) 2015-2016 Parallels IP Holdings GmbH. + * + * Author: Pavel Butsykin + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qemu/rbcache.h" + +typedef struct TestRBCacheData { + RBCache *cache; +} TestRBCacheData; + +typedef struct TestRBCacheConfig { + uint64_t limit_size; + int eviction_type; + RBNodeAlloc *alloc; + RBNodeFree *free; + void *opaque; +} TestRBCacheConfig; + +#define KB(_n) ((_n) << 10) +#define MB(_n) ((_n) << 20) + +#define OFFSET1 0 +#define SIZE1 KB(1) + +#define OFFSET2 KB(1) +#define SIZE2 KB(2) + +#define OFFSET3 KB(18) +#define SIZE3 KB(1) + +#define OFFSET4 KB(7) +#define SIZE4 KB(7) + +#define OFFSET5 KB(1) +#define SIZE5 KB(4) + +#define OFFSET6 KB(5) +#define SIZE6 KB(5) + +#define OFFSET7 KB(15) +#define SIZE7 KB(20) + +#define OFFSET8 KB(2) +#define SIZE8 KB(20) + + +static void test_rbcache_init(TestRBCacheData *data, const void *ctx) +{ + g_assert_nonnull(data->cache); +} + +static void test_rbcache_insert(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node1 = rbcache_node_alloc(data->cache, OFFSET1, SIZE1); + RBCacheNode *node2 = rbcache_node_alloc(data->cache, OFFSET2, SIZE2); + RBCacheNode *node3 = rbcache_node_alloc(data->cache, OFFSET3, SIZE3); + RBCacheNode *node4 = rbcache_node_alloc(data->cache, OFFSET4, SIZE4); + RBCacheNode *node5 = rbcache_node_alloc(data->cache, OFFSET5, SIZE5); + RBCacheNode *node6 = rbcache_node_alloc(data->cache, OFFSET6, SIZE6); + RBCacheNode *node7 = rbcache_node_alloc(data->cache, OFFSET7, SIZE7); + RBCacheNode *node8 = rbcache_node_alloc(data->cache, OFFSET8, SIZE8); + RBCacheNode *node; + + node = rbcache_insert(data->cache, node2); + g_assert_true(node == node2); + + node = rbcache_insert(data->cache, node1); + g_assert_true(node == node1); + + node = rbcache_insert(data->cache, node3); + g_assert_true(node == node3); + + node = rbcache_insert(data->cache, node4); + g_assert_true(node == node4); + + node = rbcache_insert(data->cache, node5); + g_assert_true(node == node2); + rbcache_node_free(data->cache, node5); + + node = rbcache_insert(data->cache, node6); + g_assert_true(node == node4); + rbcache_node_free(data->cache, node6); + + node = rbcache_insert(data->cache, node7); + g_assert_true(node == node3); + rbcache_node_free(data->cache, node7); + + node = rbcache_insert(data->cache, node8); + g_assert_true(node == node2); + rbcache_node_free(data->cache, node8); +} + +static void test_rbcache_search(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + test_rbcache_insert(data, ctx); + + node = rbcache_search(data->cache, OFFSET1, SIZE1); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET1); + g_assert_cmpuint(node->bytes, ==, SIZE1); + + node = rbcache_search(data->cache, OFFSET2 + KB(1), SIZE2); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); + + node = rbcache_search(data->cache, OFFSET8, SIZE8); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); + + node = rbcache_search(data->cache, OFFSET8 + KB(2), SIZE5); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET4); + g_assert_cmpuint(node->bytes, ==, OFFSET4); + + node = rbcache_search(data->cache, OFFSET3 + SIZE3, SIZE3); + g_assert_null(node); +} + +static void test_rbcache_search_and_insert(TestRBCacheData *data, + const void *ctx) +{ + RBCacheNode *node; + + node = rbcache_search_and_insert(data->cache, OFFSET2, SIZE2); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); + + node = rbcache_search_and_insert(data->cache, OFFSET1, SIZE1); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET1); + g_assert_cmpuint(node->bytes, ==, SIZE1); + + node = rbcache_search_and_insert(data->cache, OFFSET3, SIZE3); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET3); + g_assert_cmpuint(node->bytes, ==, SIZE3); + + node = rbcache_search_and_insert(data->cache, OFFSET4, SIZE4); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET4); + g_assert_cmpuint(node->bytes, ==, SIZE4); + + node = rbcache_search_and_insert(data->cache, OFFSET5, SIZE5); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); + + node = rbcache_search_and_insert(data->cache, OFFSET6, SIZE6); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET4); + g_assert_cmpuint(node->bytes, ==, SIZE4); + + node = rbcache_search_and_insert(data->cache, OFFSET7, SIZE7); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET3); + g_assert_cmpuint(node->bytes, ==, SIZE3); + + node = rbcache_search_and_insert(data->cache, OFFSET8, SIZE8); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); +} + +static void test_rbcache_remove(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + test_rbcache_search_and_insert(data, ctx); + + node = rbcache_search(data->cache, OFFSET1, SIZE1); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET1, SIZE1); + g_assert_null(node); + + node = rbcache_search(data->cache, OFFSET3, SIZE3); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET3, SIZE3); + g_assert_null(node); + + node = rbcache_search(data->cache, OFFSET4, SIZE4); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET4, SIZE4); + g_assert_null(node); + + node = rbcache_search(data->cache, OFFSET2, SIZE2); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET2, SIZE2); + g_assert_null(node); +} + +static void test_rbcache_shrink(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + node = rbcache_search_and_insert(data->cache, 0, MB(2)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(2), MB(3)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, 0, MB(2)); + g_assert_null(node); + + node = rbcache_search(data->cache, MB(2), MB(3)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, 0, MB(2)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, 0, MB(2)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, MB(2), MB(3)); + g_assert_null(node); +} + +static void test_rbcache_shrink_fifo(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + rbcache_search_and_insert(data->cache, 0, MB(1)); + rbcache_search_and_insert(data->cache, MB(1), MB(1)); + rbcache_search_and_insert(data->cache, MB(2), MB(1)); + rbcache_search_and_insert(data->cache, MB(3), MB(1)); + + node = rbcache_search_and_insert(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(7), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(7), MB(1)); + g_assert_nonnull(node); +} + +static void test_rbcache_shrink_lru(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + rbcache_search_and_insert(data->cache, 0, MB(1)); + rbcache_search_and_insert(data->cache, MB(1), MB(1)); + rbcache_search_and_insert(data->cache, MB(2), MB(1)); + rbcache_search_and_insert(data->cache, MB(3), MB(1)); + + node = rbcache_search_and_insert(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(7), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_null(node); + node = rbcache_search(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(7), MB(1)); + g_assert_nonnull(node); +} + +static void test_rbcache_setup(TestRBCacheData *data, const void *ctx) +{ + const TestRBCacheConfig *config = ctx; + + data->cache = + rbcache_create(config->alloc, config->free, config->limit_size, + config->eviction_type, config->opaque); +} + +static void test_rbcache_teardown(TestRBCacheData *data, const void *ctx) +{ + rbcache_destroy(data->cache); +} + +static void rbcache_test_add(const char *testpath, + void (*test_func)(TestRBCacheData *data, + const void *user_data), void *ctx) +{ + g_test_add(testpath, TestRBCacheData, ctx, test_rbcache_setup, test_func, + test_rbcache_teardown); +} + +int main(int argc, char **argv) +{ + TestRBCacheConfig config = { + .limit_size = MB(4), + .eviction_type = RBCACHE_FIFO, + }; + TestRBCacheConfig config_lru = { + .limit_size = MB(4), + .eviction_type = RBCACHE_LRU, + }; + + g_test_init(&argc, &argv, NULL); + + rbcache_test_add("/rbcache/init", test_rbcache_init, &config); + rbcache_test_add("/rbcache/insert", test_rbcache_insert, &config); + rbcache_test_add("/rbcache/search", test_rbcache_search, &config); + rbcache_test_add("/rbcache/search_and_insert", + test_rbcache_search_and_insert, &config); + rbcache_test_add("/rbcache/rbcache_remove", test_rbcache_remove, &config); + rbcache_test_add("/rbcache/shrink", test_rbcache_shrink, &config); + rbcache_test_add("/rbcache/shrink/fifo", test_rbcache_shrink_fifo, &config); + rbcache_test_add("/rbcache/shrink/lru", test_rbcache_shrink_lru, + &config_lru); + + g_test_run(); + + return 0; +}