@Test public void testAttributeOrder() { for (List<String> strings: someNonEmptyLists(uniqueValues(nonEmptyStrings(), 10))) { DataSetBuilder bld = new DataSetBuilder(nonEmptyStrings().next()); StaticDataSource train = new StaticDataSource("train"); bld.setTrain(train); StaticDataSource test = new StaticDataSource("test"); bld.setTest(test); for (String str: strings) { bld.setAttribute(str, nonEmptyStrings().next()); } DataSet ds = bld.build(); assertThat(ds.getAttributes().size(), equalTo(strings.size() + 1)); List<String> stringArray = new ArrayList<>(); stringArray.add("DataSet"); stringArray.addAll(strings); String[] strs = stringArray.toArray(new String[stringArray.size()]); assertThat(ds.getAttributes().keySet(), contains(strs)); } } }
/** * Use random lists to test that we always find the *first* matching element. */ @Test public void testRandomSearchesFindFirst() { for (List<String> keys: someNonEmptyLists(strings())) { Collections.sort(keys); // pick an element to duplicate int toDup = integers(0, keys.size() - 1).next(); // and # of times to duplicate int ndups = integers(0, 10).next(); String dupKey = keys.get(toDup); for (int i = 0; i < ndups; i++) { keys.add(toDup, dupKey); } BinarySearch search = BinarySearch.forList(dupKey, keys); int rv = search.search(0, keys.size()); assertThat(rv, greaterThanOrEqualTo(0)); assertThat(search.search(0, keys.size()), lessThanOrEqualTo(Collections.binarySearch(keys, dupKey))); assertThat(keys.get(rv), equalTo(dupKey)); if (rv > 0) { // this is the first one assertThat(keys.get(rv-1), lessThan(dupKey)); } } }