<commit_before>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"

#include "webkit/tools/test_shell/image_decoder_unittest.h"

#include "base/file_util.h"
#include "base/md5.h"
#include "base/path_service.h"
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "base/time.h"

using base::Time;

void ReadFileToVector(const std::wstring& path, Vector<char>* contents) {
  std::string contents_str;
  file_util::ReadFileToString(path, &contents_str);
  contents->resize(contents_str.size());
  memcpy(&contents->first(), contents_str.data(), contents_str.size());
}

std::wstring GetMD5SumPath(const std::wstring& path) {
  static const std::wstring kDecodedDataExtension(L".md5sum");
  return path + kDecodedDataExtension;
}

#ifdef CALCULATE_MD5_SUMS
void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) {
  // Create the file to write.
  ScopedHandle handle(CreateFile(path.c_str(), GENERIC_WRITE, 0,
                                 NULL, CREATE_ALWAYS,
                                 FILE_ATTRIBUTE_NORMAL, NULL));
  ASSERT_TRUE(handle.IsValid());

  // Calculate MD5 sum.
  MD5Digest digest;
  SkAutoLockPixels bmp_lock(buffer->bitmap()); 
  MD5Sum(buffer->bitmap().getPixels(),
         buffer->rect().width() * buffer->rect().height() * sizeof(unsigned),
         &digest);

  // Write sum to disk.
  int bytes_written = file_util::WriteFile(path, &digest, sizeof digest);
  ASSERT_EQ(sizeof digest, bytes_written);
}
#else
void VerifyImage(WebCore::ImageDecoder* decoder,
                 const std::wstring& path,
                 const std::wstring& md5_sum_path) {
  // Make sure decoding can complete successfully.
  EXPECT_TRUE(decoder->isSizeAvailable()) << path;
  WebCore::RGBA32Buffer* image_buffer = decoder->frameBufferAtIndex(0);
  ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << path;
  EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) <<
      path;
  EXPECT_FALSE(decoder->failed()) << path;

  // Calculate MD5 sum.
  MD5Digest actual_digest;
  SkAutoLockPixels bmp_lock(image_buffer->bitmap()); 
  MD5Sum(image_buffer->bitmap().getPixels(), image_buffer->rect().width() *
      image_buffer->rect().height() * sizeof(unsigned), &actual_digest);

  // Read the MD5 sum off disk.
  std::string file_bytes;
  file_util::ReadFileToString(md5_sum_path, &file_bytes);
  MD5Digest expected_digest;
  ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path;
  memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest);

  // Verify that the sums are the same.
  EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) <<
      path;
}
#endif

void ImageDecoderTest::SetUp() {
  ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir_));
  file_util::AppendToPath(&data_dir_, L"webkit");
  file_util::AppendToPath(&data_dir_, L"data");
  file_util::AppendToPath(&data_dir_, format_ + L"_decoder");
  ASSERT_TRUE(file_util::PathExists(data_dir_));
}

std::vector<std::wstring> ImageDecoderTest::GetImageFiles() const {
  std::wstring pattern = L"*." + format_;

  file_util::FileEnumerator enumerator(data_dir_, false,
                                       file_util::FileEnumerator::FILES);

  std::vector<std::wstring> image_files;
  std::wstring next_file_name;
  while ((next_file_name = enumerator.Next()) != L"") {
    if (!MatchPattern(next_file_name, pattern)) {
      continue;
    }
    std::wstring image_path = data_dir_;
    file_util::AppendToPath(&image_path, next_file_name);
    image_files.push_back(image_path);
  }

  return image_files;
}

bool ImageDecoderTest::ShouldImageFail(const std::wstring& path) const {
  static const std::wstring kBadSuffix(L".bad.");
  return (path.length() > (kBadSuffix.length() + format_.length()) &&
          !path.compare(path.length() - format_.length() - kBadSuffix.length(),<commit_msg>Quick fix for image decoder.<commit_after>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"

#include "webkit/tools/test_shell/image_decoder_unittest.h"

#include "base/file_util.h"
#include "base/md5.h"
#include "base/path_service.h"
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "base/time.h"

using base::Time;

void ReadFileToVector(const std::wstring& path, Vector<char>* contents) {
  std::string contents_str;
  file_util::ReadFileToString(path, &contents_str);
  contents->resize(contents_str.size());
  memcpy(&contents->first(), contents_str.data(), contents_str.size());
}

std::wstring GetMD5SumPath(const std::wstring& path) {
  static const std::wstring kDecodedDataExtension(L".md5sum");
  return path + kDecodedDataExtension;
}

#ifdef CALCULATE_MD5_SUMS
void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) {
  // Create the file to write.
  ScopedHandle handle(CreateFile(path.c_str(), GENERIC_WRITE, 0,
                                 NULL, CREATE_ALWAYS,
                                 FILE_ATTRIBUTE_NORMAL, NULL));
  ASSERT_TRUE(handle.IsValid());

  // Calculate MD5 sum.
  MD5Digest digest;
  SkAutoLockPixels bmp_lock(buffer->bitmap()); 
  MD5Sum(buffer->bitmap().getPixels(),
         buffer->rect().width() * buffer->rect().height() * sizeof(unsigned),
         &digest);

  // Write sum to disk.
  int bytes_written = file_util::WriteFile(path, &digest, sizeof digest);
  ASSERT_EQ(sizeof digest, bytes_written);
}
#else
void VerifyImage(WebCore::ImageDecoder* decoder,
                 const std::wstring& path,
                 const std::wstring& md5_sum_path) {
  // Make sure decoding can complete successfully.
  EXPECT_TRUE(decoder->isSizeAvailable()) << path;
  WebCore::RGBA32Buffer* image_buffer = decoder->frameBufferAtIndex(0);
  ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << path;
  EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) <<
      path;
  EXPECT_FALSE(decoder->failed()) << path;

  // Calculate MD5 sum.
  MD5Digest actual_digest;
  SkAutoLockPixels bmp_lock(image_buffer->bitmap()); 
  MD5Sum(image_buffer->bitmap().getPixels(), image_buffer->rect().width() *
      image_buffer->rect().height() * sizeof(unsigned), &actual_digest);

  // Read the MD5 sum off disk.
  std::string file_bytes;
  file_util::ReadFileToString(md5_sum_path, &file_bytes);
  MD5Digest expected_digest;
  ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path;
  memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest);

  // Verify that the sums are the same.
  EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) <<
      path;
}
#endif

void ImageDecoderTest::SetUp() {
  ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir_));
  file_util::AppendToPath(&data_dir_, L"webkit");
  file_util::AppendToPath(&data_dir_, L"data");
  file_util::AppendToPath(&data_dir_, format_ + L"_decoder");
  ASSERT_TRUE(file_util::PathExists(data_dir_));
}

std::vector<std::wstring> ImageDecoderTest::GetImageFiles() const {
  std::wstring pattern = L"*." + format_;

  file_util::FileEnumerator enumerator(data_dir_, false,
                                       file_util::FileEnumerator::FILES);

  std::vector<std::wstring> image_files;
  std::wstring next_file_name;
  while ((next_file_name = enumerator.Next()) != L"") {
    if (!MatchPattern(next_file_name, pattern)) {
      continue;
    }
    image_files.push_back(next_file_name);
  }

  return image_files;
}

bool ImageDecoderTest::ShouldImageFail(const std::wstring& path) const {
  static const std::wstring kBadSuffix(L".bad.");
  return (path.length() > (kBadSuffix.length() + format_.length()) &&
          !path.compare(path.length() - format_.length() - kBadSuffix.length(),
{'fraction_non_alphanumeric': 0.1399548532731377, 'fraction_numerical': 0.008778530223225483, 'mean_word_length': 4.576923076923077, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1454771', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2584, 'n_words': 502}