@Test
public void testMajorityLabel() throws Exception {

  // all instances have the same label
  String descriptor = Utils.randomDescriptor(rng, ATTRIBUTE_COUNT);
  int label = Utils.findLabel(descriptor);
  int label1 = rng.nextInt();
  double[][] source = Utils.randomDoublesWithSameLabel(rng, descriptor, false, 100,
      label1);
  String[] sData = Utils.double2String(source);
  
  Dataset dataset = DataLoader.generateDataset(descriptor, false, sData);
  Data data = DataLoader.loadData(dataset, sData);
  int code1 = dataset.labelCode(Double.toString(label1));
  assertEquals(code1, data.majorityLabel(rng));
  // 51/100 vectors have label2
  int label2 = label1 + 1;
  int nblabel2 = 51;
  while (nblabel2 > 0) {
    double[] vector = source[rng.nextInt(100)];
    if (vector[label] != label2) {
      vector[label] = label2;
      nblabel2--;
    }
  }
  sData = Utils.double2String(source);
  dataset = DataLoader.generateDataset(descriptor, false, sData);
  data = DataLoader.loadData(dataset, sData);
  int code2 = dataset.labelCode(Double.toString(label2));
  // label2 should be the majority label
  assertEquals(code2, data.majorityLabel(rng));
  // 50 vectors with label1 and 50 vectors with label2
  do {
    double[] vector = source[rng.nextInt(100)];
    if (vector[label] == label2) {
      vector[label] = label1;
      break;
    }
  } while (true);
  sData = Utils.double2String(source);
  
  data = DataLoader.loadData(dataset, sData);
  code1 = dataset.labelCode(Double.toString(label1));
  code2 = dataset.labelCode(Double.toString(label2));
  // majorityLabel should return label1 and label2 at random
  boolean found1 = false;
  boolean found2 = false;
  for (int index = 0; index < 10 && (!found1 || !found2); index++) {
    int major = data.majorityLabel(rng);
    if (major == code1) {
      found1 = true;
    }
    if (major == code2) {
      found2 = true;
    }
  }
  assertTrue(found1 && found2);
}