<commit_before>/*---------------------------------------------------------------------------*\
 *                                OpenSG                                     *
 *                                                                           *
 *                                                                           *
 *                  Copyright (C) 2006 by the OpenSG Forum                   *
 *                                                                           *
 *   contact: <EMAIL>, <EMAIL>, <EMAIL>          *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                License                                    *
 *                                                                           *
 * This library is free software; you can redistribute it and/or modify it   *
 * under the terms of the GNU Library General Public License as published    *
 * by the Free Software Foundation, version 2.                               *
 *                                                                           *
 * This library is distributed in the hope that it will be useful, but       *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public         *
 * License along with this library; if not, write to the Free Software       *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                Changes                                    *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
\*---------------------------------------------------------------------------*/

#include <UnitTest++.h>

#include <OpenSG/OSGFieldContainer.h>
#include <OpenSG/OSGNode.h>
#include <OpenSG/OSGNodeCore.h>
#include <OpenSG/OSGTransform.h>

#include <iostream>
#include <string>
#include <vector>

SUITE(FieldContainerTests)
{

TEST(appendTypesVector)
{
    std::vector<std::string>                     typeNames;
    std::vector<const OSG::FieldContainerType *> types;

    typeNames.push_back("Node");
    typeNames.push_back("NodeCore");
    typeNames.push_back("FieldContainer");

    OSG::appendTypesVector(typeNames, types);

    CHECK_EQUAL(types.size(), 3);
    CHECK(OSG::osgStringCmp(types[0]->getCName(),
                            OSG::Node::getClassType().getCName()          ) == 0);
    CHECK(OSG::osgStringCmp(types[1]->getCName(),
                            OSG::NodeCore::getClassType().getCName()      ) == 0);
    CHECK(OSG::osgStringCmp(types[2]->getCName(),
                            OSG::FieldContainer::getClassType().getCName()) == 0);
}

TEST(appendGroupsVector)
{
    std::vector<std::string> groupNames;
    std::vector<OSG::UInt16> groups;

    groupNames.push_back("Node");
    groupNames.push_back("NodeCores");
    groupNames.push_back("FieldContainer");

    OSG::appendGroupsVector(groupNames, groups);

    CHECK_EQUAL(groups.size(), 3);
    CHECK_EQUAL(groups[0], OSG::Node::getClassGroupId());
    CHECK_EQUAL(groups[1], OSG::NodeCore::getClassGroupId());
    CHECK_EQUAL(groups[2], OSG::FieldContainer::getClassGroupId());
}

TEST(appendTypesString)
{
    std::string                                  typesString;
    std::vector<const OSG::FieldContainerType *> types;

    typesString.assign("Node, NodeCore,FieldContainer");

    OSG::appendTypesString(typesString, types);

    CHECK_EQUAL(types.size(), 3);
    CHECK(OSG::osgStringCmp(types[0]->getCName(),
                            OSG::Node::getClassType().getCName()          ) == 0);
    CHECK(OSG::osgStringCmp(types[1]->getCName(),
                            OSG::NodeCore::getClassType().getCName()      ) == 0);
    CHECK(OSG::osgStringCmp(types[2]->getCName(),
                            OSG::FieldContainer::getClassType().getCName()) == 0);

    typesString.assign("Node");
    types.clear();

    OSG::appendTypesString(typesString, types);

    CHECK_EQUAL(types.size(), 1);
    CHECK(OSG::osgStringCmp(types[0]->getCName(),
                            OSG::Node::getClassType().getCName()) == 0);
}

TEST(checkMemoryCleanup)
{
   // Check to make sure the memory is cleaned up correctly with an FCPtr
   OSG::NodeRefPtr  node(OSG::Node::create());
   OSG::UInt32   node_id   = OSG::getContainerId(node.get());
   OSG::Int32    ref_count = getRefCount(node.get());
   OSG::commitChanges();
   CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) != OSGNullFC);

   // Now release the ref and check that it was collected
   node = OSGNullFC;
   OSG::commitChanges();
   CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) == OSGNullFC);
}

// ---- Memory Debugging Tests ---- //
#ifdef OSG_ENABLE_MEMORY_DEBUGGING

TEST(checkMemWithCoredNodes)<commit_msg>Attempt to add test for uncommitted changes problem.  This doesn't cause the issue yet, but it is the best I can come up with.<commit_after>/*---------------------------------------------------------------------------*\
 *                                OpenSG                                     *
 *                                                                           *
 *                                                                           *
 *                  Copyright (C) 2006 by the OpenSG Forum                   *
 *                                                                           *
 *   contact: <EMAIL>, <EMAIL>, <EMAIL>          *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                License                                    *
 *                                                                           *
 * This library is free software; you can redistribute it and/or modify it   *
 * under the terms of the GNU Library General Public License as published    *
 * by the Free Software Foundation, version 2.                               *
 *                                                                           *
 * This library is distributed in the hope that it will be useful, but       *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public         *
 * License along with this library; if not, write to the Free Software       *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                Changes                                    *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
\*---------------------------------------------------------------------------*/

#include <UnitTest++.h>

#include <OpenSG/OSGFieldContainer.h>
#include <OpenSG/OSGNode.h>
#include <OpenSG/OSGNodeCore.h>
#include <OpenSG/OSGTransform.h>

#include <iostream>
#include <string>
#include <vector>

SUITE(FieldContainerTests)
{

TEST(appendTypesVector)
{
    std::vector<std::string>                     typeNames;
    std::vector<const OSG::FieldContainerType *> types;

    typeNames.push_back("Node");
    typeNames.push_back("NodeCore");
    typeNames.push_back("FieldContainer");

    OSG::appendTypesVector(typeNames, types);

    CHECK_EQUAL(types.size(), 3);
    CHECK(OSG::osgStringCmp(types[0]->getCName(),
                            OSG::Node::getClassType().getCName()          ) == 0);
    CHECK(OSG::osgStringCmp(types[1]->getCName(),
                            OSG::NodeCore::getClassType().getCName()      ) == 0);
    CHECK(OSG::osgStringCmp(types[2]->getCName(),
                            OSG::FieldContainer::getClassType().getCName()) == 0);
}

TEST(appendGroupsVector)
{
    std::vector<std::string> groupNames;
    std::vector<OSG::UInt16> groups;

    groupNames.push_back("Node");
    groupNames.push_back("NodeCores");
    groupNames.push_back("FieldContainer");

    OSG::appendGroupsVector(groupNames, groups);

    CHECK_EQUAL(groups.size(), 3);
    CHECK_EQUAL(groups[0], OSG::Node::getClassGroupId());
    CHECK_EQUAL(groups[1], OSG::NodeCore::getClassGroupId());
    CHECK_EQUAL(groups[2], OSG::FieldContainer::getClassGroupId());
}

TEST(appendTypesString)
{
    std::string                                  typesString;
    std::vector<const OSG::FieldContainerType *> types;

    typesString.assign("Node, NodeCore,FieldContainer");

    OSG::appendTypesString(typesString, types);

    CHECK_EQUAL(types.size(), 3);
    CHECK(OSG::osgStringCmp(types[0]->getCName(),
                            OSG::Node::getClassType().getCName()          ) == 0);
    CHECK(OSG::osgStringCmp(types[1]->getCName(),
                            OSG::NodeCore::getClassType().getCName()      ) == 0);
    CHECK(OSG::osgStringCmp(types[2]->getCName(),
                            OSG::FieldContainer::getClassType().getCName()) == 0);

    typesString.assign("Node");
    types.clear();

    OSG::appendTypesString(typesString, types);

    CHECK_EQUAL(types.size(), 1);
    CHECK(OSG::osgStringCmp(types[0]->getCName(),
                            OSG::Node::getClassType().getCName()) == 0);
}

TEST(checkMemoryCleanup)
{
   // Check to make sure the memory is cleaned up correctly with an FCPtr
   OSG::NodeRefPtr  node(OSG::Node::create());
   OSG::UInt32   node_id   = OSG::getContainerId(node.get());
   OSG::Int32    ref_count = getRefCount(node.get());
   OSG::commitChanges();
   CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) != OSGNullFC);

   // Now release the ref and check that it was collected
   node = OSGNullFC;
   OSG::commitChanges();
   CHECK(OSG::FieldContainerFactory::the()->getContainer(node_id) == OSGNullFC);
}

// Test to show the bug where uncommitted changes was referencing old data
//
TEST(testUncommittedChangesRegression)
{
   // The idea here is to try to force a subref/addref change using an old
   // entry that would contain invalid data
   OSG::NodeRefPtr outer_node(OSG::Node::create());
   OSG::NodeRefPtr outer_node2(OSG::Node::create());
   for(unsigned i=0;i<100;i++)
   {
      if ((i%5) == 0)
      { outer_node = OSG::NodeRefPtr(OSG::Node::create()); }

      OSG::NodeRefPtr temp_node = outer_node2;
      OSG::NodeRefPtr temp_node2 = outer_node2;

      OSG::commitChanges();
      OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
   }   
}

// ---- Memory Debugging Tests ---- //
#ifdef OSG_ENABLE_MEMORY_DEBUGGING

TEST(checkMemWithCoredNodes)
{'fraction_non_alphanumeric': 0.17798033101463182, 'fraction_numerical': 0.007195970256656272, 'mean_word_length': 1.1920785138450753, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 98, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3433396', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2830, 'n_words': 576}