Esempio n. 1
0
static const ONX_ErrorCounter Internal_TestReadFolder(
  ON_TextLog& text_log,
  const wchar_t* directory_path,
  unsigned int directory_tree_depth,
  bool bVerbose,
  Internal_CTestContext& test_context
  )
{
  ONX_ErrorCounter error_counter;
  error_counter.ClearLibraryErrors();

  if (nullptr == directory_path || 0 == directory_path[0])
  {
    text_log.Print(L"Empty directory name.\n");
  }

  ON_FileIterator fit;
  if (false == fit.Initialize(directory_path))
  {
    text_log.Print(
      L"Invalid directory name: %ls\n",
      directory_path
      );
    error_counter.IncrementFailureCount();
    return error_counter;
  }

  const ON_wString text_log_directory_name
    = (directory_tree_depth <= 1)
    ? ON_wString(directory_path)
    : test_context.TextLogPathFromFullPath(directory_path);
  text_log.Print(
    L"Directory name: %ls\n",
    static_cast<const wchar_t*>(text_log_directory_name)
    );
  text_log.PushIndent();

  ON_ClassArray< ON_wString > sub_directories(32);
  ON_ClassArray< ON_wString > skipped_files(32);
  ON_ClassArray< ON_wString > tested_files(32);

  for ( bool bHaveItem = fit.FirstItem(); bHaveItem; bHaveItem = fit.NextItem() )
  {
    if ( test_context.m_max_file_count > 0 && test_context.m_file_count >= test_context.m_max_file_count)
      break;

    if (fit.CurrentItemIsDirectory())
    {
      if (directory_tree_depth < test_context.m_max_directory_tree_depth)
      {
        sub_directories.Append(fit.CurrentItemFullPathName());
      }
      continue;
    }

    ON_wString fullpath = fit.CurrentItemFullPathName();

    if (ON_FileStream::Is3dmFile(fullpath, false))
      tested_files.Append(fullpath);
    else
      skipped_files.Append(fullpath);
  }

  // sort file and folder names so test order depends only on content.
  // This is required so results from different computers can be compared.
  sub_directories.QuickSort(Internal_ComparePath);
  skipped_files.QuickSort(Internal_ComparePath);
  tested_files.QuickSort(Internal_ComparePath);

  if (skipped_files.Count() > 0)
  {
    text_log.PrintNewLine();
    for (int i = 0; i < skipped_files.Count(); i++)
    {
      const ON_wString path_to_print = test_context.TextLogPathFromFullPath(skipped_files[i]);
      text_log.Print(
        L"Skipped file: %ls\n",
        static_cast<const wchar_t*>(path_to_print)
      );
    }
    text_log.PrintNewLine();
  }
     
  for ( int i = 0; i < tested_files.Count(); i++ )
  {
    const ON_wString full_path = tested_files[i];
    const ON_wString path_to_print = test_context.TextLogPathFromFullPath(full_path);
    test_context.m_file_count++;
    const ONX_ErrorCounter file_error_counter = Internal_TestFileRead(text_log, full_path, path_to_print, bVerbose);
    error_counter += file_error_counter;
  }

  for (int i = 0; i < sub_directories.Count(); i++)
  {
    if (test_context.m_max_file_count > 0 && test_context.m_file_count >= test_context.m_max_file_count)
      break;
    const ON_wString sub_directory_path = sub_directories[i];
    test_context.m_directory_count++;
    error_counter += Internal_TestReadFolder(text_log, sub_directory_path, directory_tree_depth + 1, bVerbose, test_context);
  }

  text_log.PopIndent();

  return error_counter;
}
Esempio n. 2
0
static const ONX_ErrorCounter Internal_TestModelRead(
  ON_TextLog& text_log,
  const ON_wString text_log_3dm_file_name,
  ON_BinaryArchive& source_archive,
  bool bVerbose
  )
{
  ONX_ModelTest::Type test_type = ONX_ModelTest::Type::ReadWriteReadCompare;
  
  /////
  //
  // Read the orginal file
  //
  text_log.PushIndent();
  ONX_ModelTest test;
  test.ReadTest(source_archive, test_type, true, text_log_3dm_file_name, &text_log);
  text_log.PrintNewLine();
  text_log.Print("Test Results:\n");
  text_log.PushIndent();
  test.Dump(text_log);
  text_log.PopIndent();

  ONX_ErrorCounter error_counter = test.ErrorCounter();

  const ONX_Model* source_model = test.SourceModel().get();
  if (nullptr == source_model)
  {
    text_log.PopIndent();
    return error_counter;
  }

  const bool bCompareTestFailed = ONX_ModelTest::Result::Fail == test.TestResult(ONX_ModelTest::Type::ReadWriteReadCompare);

  if ( bVerbose || bCompareTestFailed )
  {
    for (int i = 0; i < 2; i++)
    {
      if (0 == i)
        test.DumpSourceModel();
      else
        test.DumpReadWriteReadModel();
      if (false == bCompareTestFailed)
        break;
    }
  }
  
  text_log.PrintNewLine();

  const ON_ModelComponent::Type component_types[] =
  {
    // TODO uncomment types as components are derived from ON_ModelComponent
    ON_ModelComponent::Type::Image,
    //ON_ModelComponent::Type::TextureMapping,
    ON_ModelComponent::Type::RenderMaterial,
    ON_ModelComponent::Type::LinePattern,
    ON_ModelComponent::Type::Layer,
    //ON_ModelComponent::Type::Group,
    ON_ModelComponent::Type::TextStyle,
    ON_ModelComponent::Type::DimStyle,
    ON_ModelComponent::Type::RenderLight,
    ON_ModelComponent::Type::HatchPattern,
    ON_ModelComponent::Type::InstanceDefinition,
    ON_ModelComponent::Type::ModelGeometry,
    //ON_ModelComponent::Type::HistoryRecord
  };

  const unsigned int component_type_count = (unsigned int)(sizeof(component_types) / sizeof(component_types[0]));

  const ONX_Model& model = *source_model;

  error_counter.ClearLibraryErrorsAndWarnings();

  const ON_ComponentManifest& manifest = model.Manifest();
  ON_SerialNumberMap it_map;

  for (unsigned int i = 0; i < component_type_count; i++)
  {
    const ON_ModelComponent::Type component_type = component_types[i];
    const bool bUniqueNameRequired = ON_ModelComponent::UniqueNameRequired(component_type);
    const bool bEmbeddedFileComponent = (ON_ModelComponent::Type::Image == component_type);
    ONX_ModelComponentIterator it(model,component_type);
    unsigned int it_count = 0;
    for (ON_ModelComponentReference mcr = it.FirstComponentReference(); false == mcr.IsEmpty(); mcr = it.NextComponentReference())
    {
      const ON_ModelComponent* model_component = mcr.ModelComponent();
      if (nullptr == model_component)
      {
        ON_ERROR("Iterator returned nullptr mcr.ModelComponent()");
        continue; 
      }
      if (model_component->ComponentType() != component_type)
      {
        ON_ERROR("Iterator returned wrong mcr.ModelComponent()->ComponentType()");
        continue; 
      }
      const ON__UINT64 model_component_sn = model_component->RuntimeSerialNumber();
      const ON_UUID model_component_id = model_component->Id();
      if (0 == model_component_sn)
      {
        ON_ERROR("Iterator mcr.ModelComponent()->RuntimeSerialNumber() is zero.");
        continue; 
      }
      if (ON_nil_uuid == model_component_id)
      {
        ON_ERROR("Iterator mcr.ModelComponent()->Id() is nil.");
        continue; 
      }
      const ON_SerialNumberMap::SN_ELEMENT* e_sn = it_map.FindSerialNumber(model_component_sn);
      if (nullptr != e_sn)
      {
        ON_ERROR("Iterator returned component serial number twice.");
        continue; 
      }
      const ON_SerialNumberMap::SN_ELEMENT* e_id = it_map.FindId(model_component_id);
      if (nullptr != e_id)
      {
        ON_ERROR("Iterator returned component id twice.");
        continue; 
      }
      ON_SerialNumberMap::SN_ELEMENT* e = it_map.AddSerialNumberAndId(model_component_sn,model_component_id);
      if (nullptr == e)
      {
        ON_ERROR("ON_SerialNumberMap failed to add sn and id.");
        continue; 
      }
      if (e->m_value.m_u_type != 0)
      {
        ON_ERROR("ON_SerialNumberMap error.");
        continue; 
      }
      e->m_value.m_u_type = 1;
      e->m_value.m_u.ptr = (void*)model_component;

      const ON_ComponentManifestItem& item = manifest.ItemFromComponentRuntimeSerialNumber(model_component_sn);
      if (item.IsUnset())
      {
        ON_ERROR("Iterator returned item not in manifest.");
        continue; 
      }
      if (model_component_sn != item.ComponentRuntimeSerialNumber())
      {
        ON_ERROR("manifest.ItemFromComponentRuntimeSerialNumber() error.");
        continue; 
      }
      if (model_component_id != item.Id())
      {
        ON_ERROR("item has wrong id.");
        continue; 
      }
 
      const ON_ComponentManifestItem& item_id = manifest.ItemFromId(model_component_id);
      if (&item != &item_id)
      {
        ON_ERROR("manifest.ItemFromId() failed.");
        continue; 
      }

      if (bUniqueNameRequired || bEmbeddedFileComponent)
      {
        const ON_Bitmap* embedded_file_reference = bEmbeddedFileComponent ? ON_Bitmap::Cast(model_component) : nullptr;
        const ON_NameHash full_path_hash 
          = (nullptr != embedded_file_reference) 
          ? ON_NameHash::CreateFilePathHash(embedded_file_reference->FileReference()) 
          : ON_NameHash::EmptyNameHash;
        const ON_NameHash name_hash 
          =bEmbeddedFileComponent
          ? full_path_hash
          : model_component->NameHash();
        if (false == name_hash.IsValidAndNotEmpty())
        {
          ON_ERROR("model compoent name is not valid.");
          continue; 
        }
        if (name_hash != item.NameHash())
        {
          ON_ERROR("item has wrong name hash.");
          continue; 
        }
        const ON_ComponentManifestItem& item_name = manifest.ItemFromNameHash(component_type,name_hash);
        if (&item != &item_name)
        {
          ON_ERROR("manifest.ItemFromNameHash() failed.");
          continue; 
        }
      }
      it_count++;
    }

    if (it_count != manifest.ActiveAndDeletedComponentCount(component_type))
    {
      ON_ERROR("iterator count != manifest.ActiveAndDeletedComponentCount(component_type)");
      continue; 
    }
  }

  text_log.PopIndent();
  error_counter.AddLibraryErrorsAndWarnings();
  return error_counter;
}