void TestSetActiveLayer()
  {
    // Cache active layer
    mitk::LabelSet::ConstPointer activeLayer = m_LabelSetImage->GetActiveLabelSet();

    // Add new layer
    mitk::LabelSet::Pointer newlayer = mitk::LabelSet::New();
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    label1->SetValue(1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    label2->SetValue(200);

    newlayer->AddLabel(label1);
    newlayer->AddLabel(label2);
    newlayer->SetActiveLabel(200);

    unsigned int layerID = m_LabelSetImage->AddLayer(newlayer);

    // Set initial layer as active layer
    m_LabelSetImage->SetActiveLayer(0);
    CPPUNIT_ASSERT_MESSAGE("Wrong active labelset returned", mitk::Equal(*activeLayer, *m_LabelSetImage->GetActiveLabelSet(), 0.00001, true));

    // Set previously added layer as active layer
    m_LabelSetImage->SetActiveLayer(layerID);
    CPPUNIT_ASSERT_MESSAGE("Wrong active labelset returned", mitk::Equal(*newlayer, *m_LabelSetImage->GetActiveLabelSet(), 0.00001, true));

    // Set a non existing layer as active layer - nothing should change
    m_LabelSetImage->SetActiveLayer(10000);
    CPPUNIT_ASSERT_MESSAGE("Wrong active labelset returned", mitk::Equal(*newlayer, *m_LabelSetImage->GetActiveLabelSet(), 0.00001, true));
  }
  void TestRemoveLayer()
  {
    // Cache active layer
    mitk::LabelSet::ConstPointer activeLayer = m_LabelSetImage->GetActiveLabelSet();

    // Add new layers
    m_LabelSetImage->AddLayer();

    mitk::LabelSet::Pointer newlayer = mitk::LabelSet::New();
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    label1->SetValue(1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    label2->SetValue(200);

    newlayer->AddLabel(label1);
    newlayer->AddLabel(label2);
    newlayer->SetActiveLabel(200);

    m_LabelSetImage->AddLayer(newlayer);

    CPPUNIT_ASSERT_MESSAGE("Wrong active labelset returned",
                           mitk::Equal(*newlayer, *m_LabelSetImage->GetActiveLabelSet(), 0.00001, true));

    m_LabelSetImage->RemoveLayer();
    CPPUNIT_ASSERT_MESSAGE("Wrong number of layers, after a layer was removed",
                           m_LabelSetImage->GetNumberOfLayers() == 2);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(2) == false);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(1) == true);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(0) == true);

    m_LabelSetImage->RemoveLayer();
    CPPUNIT_ASSERT_MESSAGE("Wrong number of layers, after a layer was removed",
                           m_LabelSetImage->GetNumberOfLayers() == 1);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(1) == false);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(0) == true);
    CPPUNIT_ASSERT_MESSAGE("Wrong active layer",
                           mitk::Equal(*activeLayer, *m_LabelSetImage->GetActiveLabelSet(), 0.00001, true));

    m_LabelSetImage->RemoveLayer();
    CPPUNIT_ASSERT_MESSAGE("Wrong number of layers, after a layer was removed",
                           m_LabelSetImage->GetNumberOfLayers() == 0);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(0) == false);
    CPPUNIT_ASSERT_MESSAGE("Active layers is not nullptr although all layer have been removed",
                           m_LabelSetImage->GetActiveLabelSet() == 0);
  }
  void TestExistsLabelSet()
  {
    // Cache active layer
    mitk::LabelSet::ConstPointer activeLayer = m_LabelSetImage->GetActiveLabelSet();

    // Add new layer
    mitk::LabelSet::Pointer newlayer = mitk::LabelSet::New();
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    label1->SetValue(1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    label2->SetValue(200);

    newlayer->AddLabel(label1);
    newlayer->AddLabel(label2);
    newlayer->SetActiveLabel(200);

    m_LabelSetImage->AddLayer(newlayer);

    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(0) == true);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(1) == true);
    CPPUNIT_ASSERT_MESSAGE("Check for existing layer failed", m_LabelSetImage->ExistLabelSet(20) == false);
  }
  void TestGetLabel()
  {
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    mitk::Label::PixelType value1 = 1;
    label1->SetValue(value1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    mitk::Label::PixelType value2 = 200;
    label2->SetValue(value2);

    m_LabelSetImage->GetActiveLabelSet()->AddLabel(label1);
    m_LabelSetImage->AddLayer();
    m_LabelSetImage->GetLabelSet(1)->AddLabel(label2);

    CPPUNIT_ASSERT_MESSAGE("Wrong label retrieved for active layer",
                           mitk::Equal(*m_LabelSetImage->GetLabel(1), *label1, 0.0001, true));
    CPPUNIT_ASSERT_MESSAGE("Wrong label retrieved for layer 1",
                           mitk::Equal(*m_LabelSetImage->GetLabel(200, 1), *label2, 0.0001, true));

    // Try to get a non existing label
    mitk::Label *label3 = m_LabelSetImage->GetLabel(1000);
    CPPUNIT_ASSERT_MESSAGE("Non existing label should be nullptr", label3 == nullptr);

    // Try to get a label from a non existing layer
    label3 = m_LabelSetImage->GetLabel(200, 1000);
    CPPUNIT_ASSERT_MESSAGE("Label from non existing layer should be nullptr", label3 == nullptr);
  }
  void TestGetActiveLabel()
  {
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    mitk::Label::PixelType value1 = 1;
    label1->SetValue(value1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    mitk::Label::PixelType value2 = 200;
    label2->SetValue(value2);

    m_LabelSetImage->GetActiveLabelSet()->AddLabel(label1);
    m_LabelSetImage->GetActiveLabelSet()->AddLabel(label2);
    m_LabelSetImage->GetActiveLabelSet()->SetActiveLabel(1);
    CPPUNIT_ASSERT_MESSAGE("Layer was not added correctly to image - active label is wrong",m_LabelSetImage->GetActiveLabel()->GetValue() == value1);
    m_LabelSetImage->GetActiveLabelSet()->SetActiveLabel(value2);
    CPPUNIT_ASSERT_MESSAGE("Layer was not added correctly to image - active label is wrong",m_LabelSetImage->GetActiveLabel()->GetValue() == value2);
  }
  void TestGetTotalNumberOfLabels()
  {
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    mitk::Label::PixelType value1 = 1;
    label1->SetValue(value1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    mitk::Label::PixelType value2 = 200;
    label2->SetValue(value2);

    m_LabelSetImage->GetActiveLabelSet()->AddLabel(label1);
    m_LabelSetImage->AddLayer();
    m_LabelSetImage->GetLabelSet(1)->AddLabel(label2);
    CPPUNIT_ASSERT_MESSAGE("Wrong total number of labels", m_LabelSetImage->GetTotalNumberOfLabels() == 4); // added 2 labels + 2 exterior default labels
  }
  void TestGetActiveLabelSet()
  {
    mitk::LabelSet::Pointer newlayer = mitk::LabelSet::New();
    mitk::Label::Pointer label1 = mitk::Label::New();
    label1->SetName("Label1");
    label1->SetValue(1);

    mitk::Label::Pointer label2 = mitk::Label::New();
    label2->SetName("Label2");
    label2->SetValue(200);

    newlayer->AddLabel(label1);
    newlayer->AddLabel(label2);
    newlayer->SetActiveLabel(200);

    unsigned int layerID = m_LabelSetImage->AddLayer(newlayer);

    mitk::LabelSet::Pointer activeLayer = m_LabelSetImage->GetActiveLabelSet();

    CPPUNIT_ASSERT_MESSAGE("Wrong layer ID was returned", layerID == 1);
    CPPUNIT_ASSERT_MESSAGE("Wrong active labelset returned", mitk::Equal(*newlayer, *activeLayer, 0.00001, true));
  }
  void TestMergeLabel()
  {
    mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(mitk::IOUtil::Load(GetTestDataFilePath("Multilabel/LabelSetTestInitializeImage.nrrd"))[0].GetPointer());
    m_LabelSetImage = 0;
    m_LabelSetImage = mitk::LabelSetImage::New();
    m_LabelSetImage->InitializeByLabeledImage(image);

    CPPUNIT_ASSERT_MESSAGE("Image - number of labels is not 6", m_LabelSetImage->GetNumberOfLabels() == 6);

    // 2ndMin because of the exterior label = 0
    CPPUNIT_ASSERT_MESSAGE("Wrong MIN value", m_LabelSetImage->GetStatistics()->GetScalarValueMin() == 0);
    CPPUNIT_ASSERT_MESSAGE("Wrong MAX value", m_LabelSetImage->GetStatistics()->GetScalarValueMax() == 7);

    m_LabelSetImage->GetActiveLabelSet()->SetActiveLabel(6);
    // Merge label 7 with label 0. Result should be that label 7 is not present any more
    m_LabelSetImage->MergeLabel(6, 7);
    CPPUNIT_ASSERT_MESSAGE("Label with value 7 was not remove from the image", m_LabelSetImage->GetStatistics()->GetScalarValueMax() == 6);
    m_LabelSetImage->GetStatistics()->GetScalarValue2ndMax();

    // Count all pixels with value 7 = 823
    // Count all pixels with value 6 = 507
    // Check if merge label has 507 + 823 = 1330 pixels
    CPPUNIT_ASSERT_MESSAGE("Label with value 7 was not remove from the image", m_LabelSetImage->GetStatistics()->GetCountOfMaxValuedVoxels() == 1330);
  }