summaryrefslogtreecommitdiffstats
path: root/courgette/difference_estimator_unittest.cc
blob: 3932e0422e530435e209f8fb3a8f027f28164081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2009 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 "courgette/difference_estimator.h"

#include <string>

#include "testing/gtest/include/gtest/gtest.h"
#include "courgette/region.h"

using courgette::DifferenceEstimator;
using courgette::Region;

TEST(DifferenceEstimatorTest, TestSame) {
  static const char kString1[] = "Hello world";
  // kString2 is stack allocated to prevent string sharing.
  const char kString2[] = "Hello world";
  DifferenceEstimator difference_estimator;
  DifferenceEstimator::Base* base =
      difference_estimator.MakeBase(Region(kString1, sizeof(kString1)));
  DifferenceEstimator::Subject* subject =
      difference_estimator.MakeSubject(Region(kString2, sizeof(kString2)));
  EXPECT_EQ(0U, difference_estimator.Measure(base, subject));
}

TEST(DifferenceEstimatorTest, TestDifferent) {
  static const char kString1[] = "Hello world";
  static const char kString2[] = "Hello universe";
  DifferenceEstimator difference_estimator;
  DifferenceEstimator::Base* base =
      difference_estimator.MakeBase(Region(kString1, sizeof(kString1)));
  DifferenceEstimator::Subject* subject =
      difference_estimator.MakeSubject(Region(kString2, sizeof(kString2)));
  EXPECT_EQ(10U, difference_estimator.Measure(base, subject));
}

TEST(DifferenceEstimatorTest, TestDifferentSuperstring) {
  static const char kString1[] = "abcdabcdabcd";
  static const char kString2[] = "abcdabcdabcdabcd";
  DifferenceEstimator difference_estimator;
  DifferenceEstimator::Base* base =
      difference_estimator.MakeBase(Region(kString1, sizeof(kString1)-1));
  DifferenceEstimator::Subject* subject =
      difference_estimator.MakeSubject(Region(kString2, sizeof(kString2)-1));
  EXPECT_EQ(1U, difference_estimator.Measure(base, subject));
}

TEST(DifferenceEstimatorTest, TestDifferentSubstring) {
  static const char kString1[] = "abcdabcdabcdabcd";
  static const char kString2[] = "abcdabcdabcd";
  DifferenceEstimator difference_estimator;
  DifferenceEstimator::Base* base =
      difference_estimator.MakeBase(Region(kString1, sizeof(kString1)-1));
  DifferenceEstimator::Subject* subject =
      difference_estimator.MakeSubject(Region(kString2, sizeof(kString2)-1));
  EXPECT_EQ(1U, difference_estimator.Measure(base, subject));
}