Port t/028-sortexrun.t to C Original commit by Nick Wellnhofer, amended by Marvin Humphrey for changes to the SortExternal API.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/eb10a8c6 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/eb10a8c6 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/eb10a8c6 Branch: refs/heads/master Commit: eb10a8c6842c3483cff014a8a7b04de0ed5c0858 Parents: 73d6925 Author: Nick Wellnhofer <[email protected]> Authored: Fri Oct 18 22:15:04 2013 +0200 Committer: Marvin Humphrey <[email protected]> Committed: Tue Jul 1 12:09:49 2014 -0700 ---------------------------------------------------------------------- core/Lucy/Test/Util/TestSortExternal.c | 38 ++++++++++++++++++++++- core/Lucy/Util/BBSortEx.c | 8 +++++ core/Lucy/Util/BBSortEx.cfh | 3 ++ perl/t/028-sortexrun.t | 48 ----------------------------- 4 files changed, 48 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/eb10a8c6/core/Lucy/Test/Util/TestSortExternal.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Test/Util/TestSortExternal.c b/core/Lucy/Test/Util/TestSortExternal.c index 2e04c83..19e83cf 100644 --- a/core/Lucy/Test/Util/TestSortExternal.c +++ b/core/Lucy/Test/Util/TestSortExternal.c @@ -269,9 +269,44 @@ test_sort_random_strings(TestBatchRunner *runner) { DECREF(bytebufs); } +static void +test_run(TestBatchRunner *runner) { + VArray *letters = VA_new(26); + for (int i = 0; i < 26; ++i) { + char ch = 'a' + i; + ByteBuf *bytebuf = BB_new_bytes(&ch, 1); + VA_Push(letters, (Obj*)bytebuf); + } + BBSortEx *run = BBSortEx_new(0x1000000, letters); + BBSortEx_Set_Mem_Thresh(run, 5); + + BBSortEx_Refill(run); + TEST_INT_EQ(runner, BBSortEx_Buffer_Count(run), 5, + "Refill doesn't exceed memory threshold"); + + Obj *endpost = BBSortEx_Peek_Last(run); + ByteBuf *wanted = BB_new_bytes("e", 1); + TEST_TRUE(runner, BB_Equals(wanted, endpost), "Peek_Last"); + + VArray *elems = VA_new(26); + do { + while (BBSortEx_Buffer_Count(run) > 0) { + Obj *object = BBSortEx_Fetch(run); + VA_Push(elems, object); + } + } while (BBSortEx_Refill(run) > 0); + TEST_TRUE(runner, VA_Equals(elems, (Obj*)letters), "retrieve all elems"); + + DECREF(elems); + DECREF(wanted); + DECREF(endpost); + DECREF(letters); + DECREF(run); +} + void TestSortExternal_Run_IMP(TestSortExternal *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 16); + TestBatchRunner_Plan(runner, (TestBatch*)self, 19); srand((unsigned int)time((time_t*)NULL)); S_init_bytebufs(); @@ -281,6 +316,7 @@ TestSortExternal_Run_IMP(TestSortExternal *self, TestBatchRunner *runner) { test_sort_nothing(runner); test_sort_packed_ints(runner); test_sort_random_strings(runner); + test_run(runner); S_destroy_bytebufs(); } http://git-wip-us.apache.org/repos/asf/lucy/blob/eb10a8c6/core/Lucy/Util/BBSortEx.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Util/BBSortEx.c b/core/Lucy/Util/BBSortEx.c index c9201bb..66e0568 100644 --- a/core/Lucy/Util/BBSortEx.c +++ b/core/Lucy/Util/BBSortEx.c @@ -183,6 +183,14 @@ BBSortEx_Peek_Cache_IMP(BBSortEx *self) { return retval; } +Obj* +BBSortEx_Peek_Last_IMP(BBSortEx *self) { + BBSortExIVARS *const ivars = BBSortEx_IVARS(self); + uint32_t count = ivars->buf_max - ivars->buf_tick; + if (count == 0) { return NULL; } + return INCREF(ivars->buffer[count-1]); +} + uint32_t BBSortEx_Get_Num_Runs_IMP(BBSortEx *self) { BBSortExIVARS *const ivars = BBSortEx_IVARS(self); http://git-wip-us.apache.org/repos/asf/lucy/blob/eb10a8c6/core/Lucy/Util/BBSortEx.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Util/BBSortEx.cfh b/core/Lucy/Util/BBSortEx.cfh index 6fd8656..a43b1fd 100644 --- a/core/Lucy/Util/BBSortEx.cfh +++ b/core/Lucy/Util/BBSortEx.cfh @@ -54,6 +54,9 @@ class Lucy::Util::BBSortEx incremented VArray* Peek_Cache(BBSortEx *self); + incremented nullable Obj* + Peek_Last(BBSortEx *self); + uint32_t Get_Num_Runs(BBSortEx *self); http://git-wip-us.apache.org/repos/asf/lucy/blob/eb10a8c6/perl/t/028-sortexrun.t ---------------------------------------------------------------------- diff --git a/perl/t/028-sortexrun.t b/perl/t/028-sortexrun.t deleted file mode 100644 index f0df52a..0000000 --- a/perl/t/028-sortexrun.t +++ /dev/null @@ -1,48 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -use strict; -use warnings; -use lib 'buildlib'; - -use Test::More skip_all => 'Disabled until test ported to C'; -#use Test::More tests => 5; -use Lucy::Test; -use Clownfish qw( to_perl ); - -my $letters = Clownfish::VArray->new; -$letters->push( Clownfish::ByteBuf->new($_) ) for 'a' .. 'z'; -my $run = Lucy::Util::BBSortEx->new( external => $letters ); -$run->set_mem_thresh(5); - -my $num_in_cache = $run->refill; -is( $run->cache_count, 5, "Read_Elem puts the brakes on Refill" ); -my $endpost = $run->peek_last; -is( $endpost, 'e', "Peek_Last" ); -$endpost = Clownfish::ByteBuf->new('b'); -my $slice = $run->pop_slice($endpost); -is( scalar @$slice, 2, "Pop_Slice gets only less-than-or-equal elems" ); -@$slice = map { to_perl($_) } @$slice; -is_deeply( $slice, [qw( a b )], "Pop_Slice picks highest elems" ); - -my @got = qw( a b ); -while (1) { - $endpost = $run->peek_last; - $slice = $run->pop_slice( Clownfish::ByteBuf->new($endpost) ); - push @got, map { to_perl($_) } @$slice; - last unless $run->refill; -} -is_deeply( \@got, [ 'a' .. 'z' ], "retrieve all elems" ); -
