/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program 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 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.util;

import org.grouplens.lenskit.scored.ScoredId;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 */
public class TopNScoredItemAccumulatorTest {
    ScoredItemAccumulator accum;

    @Before
    public void createAccumulator() {
        accum = new TopNScoredItemAccumulator(3);
    }

    @Test
    public void testEmpty() {
        List<ScoredId> out = accum.finish();
        assertTrue(out.isEmpty());
    }

    @Test
    public void testAccum() {
        accum.put(5, 4.2);
        accum.put(3, 2.9);
        accum.put(2, 9.8);
        List<ScoredId> out = accum.finish();
        assertThat(out, hasSize(3));
        assertThat(out.get(0).getId(), equalTo(2L));
        assertThat(out.get(0).getScore(), equalTo(9.8));
        assertThat(out.get(1).getId(), equalTo(5L));
        assertThat(out.get(1).getScore(), equalTo(4.2));
        assertThat(out.get(2).getId(), equalTo(3L));
        assertThat(out.get(2).getScore(), equalTo(2.9));
    }

    @Test
    public void testAccumLimit() {
        accum.put(7, 1.0);
        accum.put(5, 4.2);
        accum.put(3, 2.9);
        accum.put(2, 9.8);
        accum.put(8, 2.1);
        List<ScoredId> out = accum.finish();
        assertThat(out, hasSize(3));
        assertThat(out.get(0).getId(), equalTo(2L));
        assertThat(out.get(0).getScore(), equalTo(9.8));
        assertThat(out.get(1).getId(), equalTo(5L));
        assertThat(out.get(1).getScore(), equalTo(4.2));
        assertThat(out.get(2).getId(), equalTo(3L));
        assertThat(out.get(2).getScore(), equalTo(2.9));
    }
}

@@ -1023,16 +1023,67 @@
 .util;%0A%0A
+import it.unimi.dsi.fastutil.longs.Long2DoubleMap;%0A
 import o
@@ -1238,16 +1238,62 @@
 qualTo;%0A
+import static org.hamcrest.Matchers.hasEntry;%0A
 import s
@@ -2943,11 +2943,780 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testAccumMap() %7B%0A        accum.put(5, 4.2);%0A        accum.put(3, 2.9);%0A        accum.put(2, 9.8);%0A        Long2DoubleMap out = accum.finishMap();%0A        assertThat(out.size(), equalTo(3));%0A        assertThat(out, hasEntry(2L, 9.8));%0A        assertThat(out, hasEntry(5L, 4.2));%0A        assertThat(out, hasEntry(3L, 2.9));%0A    %7D%0A%0A    @Test%0A    public void testAccumMapLimit() %7B%0A        accum.put(7, 1.0);%0A        accum.put(5, 4.2);%0A        accum.put(3, 2.9);%0A        accum.put(2, 9.8);%0A        accum.put(8, 2.1);%0A        Long2DoubleMap out = accum.finishMap();%0A        assertThat(out.size(), equalTo(3));%0A        assertThat(out, hasEntry(2L, 9.8));%0A        assertThat(out, hasEntry(5L, 4.2));%0A        assertThat(out, hasEntry(3L, 2.9));%0A    %7D
 %0A%7D%0A

Add tests for item accumulator finishMap