// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
// .NAME Test of vtkSmartPointer.
// .SECTION Description
// Tests instantiations of the vtkSmartPointer class template.

#include "vtkDebugLeaks.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkSmartPointer.h"

#include <unordered_set>
#include <vector>

#include <iostream>

int TestSmartPointer(int, char*[])
{
  int rval = 0;
  vtkIntArray* ia = vtkIntArray::New();

  // Coverage:
  unsigned int testbits = 0;
  unsigned int correctbits = 0x00000953;
  const char* tests[] = { "da2 == ia", "da2 != ia", "da2 < ia", "da2 <= ia", "da2 > ia",
    "da2 <= ia", "da2 > ia", "da2 >= ia", "da1 == 0", "da1 != 0", "da1 < 0", "da1 <= 0", "da1 > 0",
    "da1 >= 0", nullptr };

  auto da2 = vtk::MakeSmartPointer(ia); // da2 is a vtkSmartPointer<vtkIntArray>
  vtkSmartPointer<vtkDataArray> da1(da2);
  da1 = ia;
  da1 = da2;
  testbits = (testbits << 1) | ((da2 == ia) ? 1 : 0);
  testbits = (testbits << 1) | ((da2 != ia) ? 1 : 0);
  testbits = (testbits << 1) | ((da2 < ia) ? 1 : 0);
  testbits = (testbits << 1) | ((da2 <= ia) ? 1 : 0);
  testbits = (testbits << 1) | ((da2 > ia) ? 1 : 0);
  testbits = (testbits << 1) | ((da2 >= ia) ? 1 : 0);
  testbits = (testbits << 1) | ((da1 == nullptr) ? 1 : 0);
  testbits = (testbits << 1) | ((da1 != nullptr) ? 1 : 0);
  testbits = (testbits << 1) | ((da1 < nullptr) ? 1 : 0);
  testbits = (testbits << 1) | ((da1 <= nullptr) ? 1 : 0);
  testbits = (testbits << 1) | ((da1 > nullptr) ? 1 : 0);
  testbits = (testbits << 1) | ((da1 >= nullptr) ? 1 : 0);
  if (testbits != correctbits)
  {
    unsigned int diffbits = (testbits ^ correctbits);
    int bitcount = 0;
    while (tests[bitcount] != nullptr)
    {
      bitcount++;
    }
    for (int ib = 0; ib < bitcount; ++ib)
    {
      if (((diffbits >> (bitcount - ib - 1)) & 1) != 0)
      {
        std::cerr << "comparison (" << tests[ib] << ") failed!\n";
      }
    }
    rval = 1;
  }

  (*da1).SetNumberOfComponents(1);
  if (da2)
  {
    da2->SetNumberOfComponents(1);
  }
  if (!da2)
  {
    std::cerr << "da2 is nullptr!"
              << "\n";
    rval = 1;
  }
  da1 = vtkSmartPointer<vtkDataArray>::NewInstance(ia);
  da1.TakeReference(vtkIntArray::New());
  auto da4 = vtk::TakeSmartPointer(vtkIntArray::New());
  (void)da4;
  ia->Delete();

  std::vector<vtkSmartPointer<vtkIntArray>> intarrays;
  { // local scope for vtkNew object
    vtkNew<vtkIntArray> vtknew;
    vtkSmartPointer<vtkIntArray> aa(vtknew);
    intarrays.emplace_back(vtknew);
  }
  if (intarrays[0]->GetReferenceCount() != 1)
  {
    std::cerr << "Didn't properly add vtkNew object to stl vector of smart pointers\n";
    rval = 1;
  }

  // Test hash maps
  std::unordered_set<vtkSmartPointer<vtkIntArray>> hashMap;
  int N = 10;
  while (--N)
  {
    hashMap.emplace(vtkSmartPointer<vtkIntArray>::New());
  }
  for (auto& p : hashMap)
  {
    p->SetNumberOfValues(10);
  }

  // Test move constructors
  {
    vtkSmartPointer<vtkIntArray> intArray{ vtkNew<vtkIntArray>{} };
    if (intArray == nullptr || intArray->GetReferenceCount() != 1)
    {
      std::cerr << "Move constructing a vtkSmartPointer from a vtkNew "
                   "failed.\n";
      rval = 1;
    }

    vtkSmartPointer<vtkIntArray> intArrayCopy(intArray);
    if (intArrayCopy != intArray || intArray->GetReferenceCount() != 2 ||
      intArrayCopy->GetReferenceCount() != 2)
    {
      std::cerr << "Copy constructing vtkSmartPointer yielded unexpected "
                   "result.\n";
      rval = 1;
    }

    vtkSmartPointer<vtkIntArray> intArrayMoved(std::move(intArrayCopy));
    // NOLINTNEXTLINE(bugprone-use-after-move)
    if (intArrayCopy || !intArrayMoved || intArrayMoved->GetReferenceCount() != 2)
    {
      std::cerr << "Move constructing vtkSmartPointer yielded unexpected "
                   "result.\n";
      rval = 1;
    }

    vtkSmartPointer<vtkDataArray> dataArrayCopy(intArray);
    if (dataArrayCopy != intArray || intArray->GetReferenceCount() != 3 ||
      dataArrayCopy->GetReferenceCount() != 3)
    {
      std::cerr << "Cast constructing vtkSmartPointer failed.\n";
      rval = 1;
    }

    vtkSmartPointer<vtkDataArray> dataArrayMoved(std::move(intArrayMoved));
    // NOLINTNEXTLINE(bugprone-use-after-move)
    if (!dataArrayMoved || intArrayMoved || dataArrayMoved->GetReferenceCount() != 3)
    {
      std::cerr << "Cast move-constructing vtkSmartPointer failed.\n";
      rval = 1;
    }
  }

  return rval;
}
