|
| 1 | +/* |
| 2 | + * Copyright (C) 2007-2023 Sebastiano Vigna |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the GNU Lesser General Public License v2.1 or later, |
| 6 | + * which is available at |
| 7 | + * http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html, |
| 8 | + * or the Apache Software License 2.0, which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | + * or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * |
| 15 | + * SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0 |
| 16 | + */ |
| 17 | + |
| 18 | +package it.unimi.dsi.webgraph; |
| 19 | + |
| 20 | +import it.unimi.dsi.fastutil.io.FastByteArrayInputStream; |
| 21 | +import it.unimi.dsi.fastutil.objects.Object2LongArrayMap; |
| 22 | +import it.unimi.dsi.fastutil.objects.Object2LongFunction; |
| 23 | +import it.unimi.dsi.webgraph.labelling.GammaCodedIntLabel; |
| 24 | +import it.unimi.dsi.webgraph.labelling.Label; |
| 25 | +import it.unimi.dsi.webgraph.labelling.ScatteredLabelledArcsASCIIGraph; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.UnsupportedEncodingException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Iterator; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static it.unimi.dsi.webgraph.labelling.ScatteredLabelledArcsASCIIGraph.*; |
| 36 | +import static org.junit.Assert.assertArrayEquals; |
| 37 | +import static org.junit.Assert.assertEquals; |
| 38 | + |
| 39 | +public class ScatteredLabelledArcsASCIIGraphTest extends WebGraphTestCase { |
| 40 | + private static final Label prototype = new GammaCodedIntLabel("FOO"); |
| 41 | + private static final LabelMapping labelMapping = (label, st) -> ((GammaCodedIntLabel) label).value = st.hashCode(); |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testConstructor() throws IOException { |
| 45 | + |
| 46 | + ScatteredLabelledArcsASCIIGraph g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n0 2 b\n1 0 c\n1 2 d\n2 0 e\n2 1 f".getBytes("ASCII")), prototype, labelMapping); |
| 47 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 48 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 49 | + |
| 50 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("-1 15 a\n15 2 b\n2 -1 c\nOOPS!\n-1 2 d".getBytes("ASCII")), prototype, labelMapping); |
| 51 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {0, 2}, {1, 2}, {2, 0}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 52 | + assertArrayEquals(new long[]{-1, 15, 2}, g.ids); |
| 53 | + |
| 54 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("2 0 a\n2 1 b".getBytes("ASCII")), prototype, labelMapping); |
| 55 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {0, 2}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 56 | + assertArrayEquals(new long[]{2, 0, 1}, g.ids); |
| 57 | + |
| 58 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("1 2 a".getBytes("ASCII")), prototype, labelMapping); |
| 59 | + assertEquals(new ArrayListMutableGraph(2, new int[][]{{0, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 60 | + assertArrayEquals(new long[]{1, 2}, g.ids); |
| 61 | + |
| 62 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("2 1 a".getBytes("ASCII")), prototype, labelMapping); |
| 63 | + assertEquals(new ArrayListMutableGraph(2, new int[][]{{0, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 64 | + assertArrayEquals(new long[]{2, 1}, g.ids); |
| 65 | + |
| 66 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n2 1 b".getBytes("ASCII")), prototype, labelMapping); |
| 67 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 68 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 69 | + |
| 70 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("\n0 1 a\n\n2 1 b".getBytes("ASCII")), prototype, labelMapping); |
| 71 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 72 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 73 | + |
| 74 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("\n0 1 a\n# comment\n2 b\n2 1 c\n2 X d".getBytes("ASCII")), prototype, labelMapping); |
| 75 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 76 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 77 | + |
| 78 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n0 2 b\n1 0 c\n1 2 d\n2 0 e\n2 1 f".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 79 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 80 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 81 | + |
| 82 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n0 2 b\n1 0 c\n1 d \t 2 e \n2 0 f\n2 1 g".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 83 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 84 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 85 | + |
| 86 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("2 0 a\n2 1 b".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 87 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {0, 2}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 88 | + assertArrayEquals(new long[]{2, 0, 1}, g.ids); |
| 89 | + |
| 90 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("1 2 a".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 91 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(2, new int[][]{{0, 1}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 92 | + assertArrayEquals(new long[]{1, 2}, g.ids); |
| 93 | + |
| 94 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("2 1 a".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 95 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(2, new int[][]{{0, 1}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 96 | + assertArrayEquals(new long[]{2, 1}, g.ids); |
| 97 | + |
| 98 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n2 1 b".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 99 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 100 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 101 | + |
| 102 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("\n0 1 a\n\n2 1 b".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 103 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 104 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 105 | + |
| 106 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("\n0 1 a\n# comment\n2\n2 1 b\n2 X".getBytes("ASCII")), prototype, labelMapping, true, false, 1); |
| 107 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 108 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 109 | + |
| 110 | + g = new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 0 a\n0 1 b\n0 2 c\n2 2 d\n1 0 e\n1 2 f\n2 0 g\n2 1 h".getBytes("ASCII")), prototype, labelMapping, true, true, 2); |
| 111 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 112 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testConstructorWithStrings() throws IOException { |
| 119 | + final Object2LongFunction<String> map = new Object2LongArrayMap<>(); |
| 120 | + map.defaultReturnValue(-1); |
| 121 | + |
| 122 | + map.clear(); |
| 123 | + map.put("0", 0); |
| 124 | + map.put("1", 1); |
| 125 | + map.put("2", 2); |
| 126 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n0 2 b\n1 0 c\n1 2 d\n2 0 e\n2 1 f".getBytes("ASCII")), map, prototype, labelMapping, null, 3)); |
| 127 | + |
| 128 | + map.clear(); |
| 129 | + map.put("-1", 1); |
| 130 | + map.put("15", 0); |
| 131 | + map.put("2", 2); |
| 132 | + final ImmutableGraph g = new ArrayListMutableGraph(3, new int[][]{{0, 2}, {1, 0}, {1, 2}, {2, 1}}).immutableView(); |
| 133 | + assertEquals(g, new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("-1 15 a\n15 2 b\n2 -1 c\nOOPS!\n-1 2 d".getBytes("ASCII")), map, prototype, labelMapping, null, 3)); |
| 134 | + assertEquals(g, new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("-1 15 a\n15 2 b\n2 -1 c\nOOPS!\n-1 2 d\n32 2 e\n2 32 f".getBytes("ASCII")), map, prototype, labelMapping, null, 3)); |
| 135 | + |
| 136 | + map.clear(); |
| 137 | + map.put("topo", 0); |
| 138 | + map.put("cane", 1); |
| 139 | + map.put("topocane", 2); |
| 140 | + assertEquals(g, new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("topocane cane a\ncane topo b\ncane topocane c\ntopo topocane d\n".getBytes("ASCII")), map, prototype, labelMapping, null, 3)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test(expected = IllegalArgumentException.class) |
| 144 | + public void testTargetOutOfRange() throws IOException { |
| 145 | + final Object2LongFunction<String> map = new Object2LongArrayMap<>(); |
| 146 | + map.defaultReturnValue(-1); |
| 147 | + map.put("0", 0); |
| 148 | + map.put("1", 1); |
| 149 | + map.put("2", 2); |
| 150 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n0 2 b".getBytes("ASCII")), map, prototype, labelMapping, null, 2)); |
| 151 | + } |
| 152 | + |
| 153 | + @Test(expected = IllegalArgumentException.class) |
| 154 | + public void testSourceOutOfRange() throws UnsupportedEncodingException, IOException { |
| 155 | + final Object2LongFunction<String> map = new Object2LongArrayMap<>(); |
| 156 | + map.defaultReturnValue(-1); |
| 157 | + map.put("0", 0); |
| 158 | + map.put("1", 1); |
| 159 | + map.put("2", 2); |
| 160 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ScatteredLabelledArcsASCIIGraph(new FastByteArrayInputStream("0 1 a\n2 0 b".getBytes("ASCII")), map, prototype, labelMapping, null, 2)); |
| 161 | + } |
| 162 | + |
| 163 | + private static Iterator<long[]> toArcsIterator(final String s) { |
| 164 | + return Arrays.stream(s.split("\n")) |
| 165 | + .map(l -> Arrays.stream(l.split(" ")) |
| 166 | + .mapToLong(Long::parseLong).toArray() |
| 167 | + ).iterator(); |
| 168 | + } |
| 169 | + |
| 170 | + private static Iterator<Label> toLabelIterator(final String s) { |
| 171 | + Label copy = prototype.copy(); |
| 172 | + return Arrays.stream(s.split(" ")).map(l -> { |
| 173 | + labelMapping.apply(copy, l); |
| 174 | + return copy.copy(); |
| 175 | + }).iterator(); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testConstructorWithArray() throws IOException { |
| 180 | + ScatteredLabelledArcsASCIIGraph g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("0 1\n0 2\n1 0\n1 2\n2 0\n2 1"), toLabelIterator("a b c d e f"), false, false, 100, null, null); |
| 181 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 182 | + System.out.println(Arrays.toString(g.ids)); |
| 183 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 184 | + |
| 185 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("-1 15\n15 2\n2 -1\n-1 2"), toLabelIterator("a b c d"), false, false, 100, null, null); |
| 186 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {0, 2}, {1, 2}, {2, 0}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 187 | + assertArrayEquals(new long[]{-1, 15, 2}, g.ids); |
| 188 | + |
| 189 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("2 0\n2 1"), toLabelIterator("a b"), false, false, 100, null, null); |
| 190 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {0, 2}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 191 | + assertArrayEquals(new long[]{2, 0, 1}, g.ids); |
| 192 | + |
| 193 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("1 2"), toLabelIterator("a b"), false, false, 100, null, null); |
| 194 | + assertEquals(new ArrayListMutableGraph(2, new int[][]{{0, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 195 | + assertArrayEquals(new long[]{1, 2}, g.ids); |
| 196 | + |
| 197 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("2 1"), toLabelIterator("a b"), false, false, 100, null, null); |
| 198 | + assertEquals(new ArrayListMutableGraph(2, new int[][]{{0, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 199 | + assertArrayEquals(new long[]{2, 1}, g.ids); |
| 200 | + |
| 201 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("0 1\n2 1"), toLabelIterator("a b"), false, false, 100, null, null); |
| 202 | + assertEquals(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {2, 1}}).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 203 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 204 | + |
| 205 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("0 1\n0 2\n1 0\n1 2\n2 0\n2 1"), toLabelIterator("a b c d e f"), true, false, 1, null, null); |
| 206 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 207 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 208 | + |
| 209 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("2 0\n2 1"), toLabelIterator("a b"), true, false, 1, null, null); |
| 210 | + assertEquals(Transform.symmetrize(new ArrayListMutableGraph(3, new int[][]{{0, 1}, {0, 2}}).immutableView()), new ArrayListMutableGraph(g).immutableView()); |
| 211 | + assertArrayEquals(new long[]{2, 0, 1}, g.ids); |
| 212 | + |
| 213 | + g = new ScatteredLabelledArcsASCIIGraph(toArcsIterator("0 0\n0 1\n0 2\n2 2\n1 0\n1 2\n2 0\n2 1"), toLabelIterator("a b c d e f h g"), true, true, 2, null, null); |
| 214 | + assertEquals(ArrayListMutableGraph.newCompleteGraph(3, false).immutableView(), new ArrayListMutableGraph(g).immutableView()); |
| 215 | + assertArrayEquals(new long[]{0, 1, 2}, g.ids); |
| 216 | + } |
| 217 | + |
| 218 | +} |
0 commit comments