1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "courgette/difference_estimator.h" 6 7 #include <string> 8 9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "courgette/region.h" 11 12 using courgette::DifferenceEstimator; 13 using courgette::Region; 14 15 TEST(DifferenceEstimatorTest, TestSame) { 16 static const char kString1[] = "Hello world"; 17 // kString2 is stack allocated to prevent string sharing. 18 const char kString2[] = "Hello world"; 19 DifferenceEstimator difference_estimator; 20 DifferenceEstimator::Base* base = 21 difference_estimator.MakeBase(Region(kString1, sizeof(kString1))); 22 DifferenceEstimator::Subject* subject = 23 difference_estimator.MakeSubject(Region(kString2, sizeof(kString2))); 24 EXPECT_EQ(0U, difference_estimator.Measure(base, subject)); 25 } 26 27 TEST(DifferenceEstimatorTest, TestDifferent) { 28 static const char kString1[] = "Hello world"; 29 static const char kString2[] = "Hello universe"; 30 DifferenceEstimator difference_estimator; 31 DifferenceEstimator::Base* base = 32 difference_estimator.MakeBase(Region(kString1, sizeof(kString1))); 33 DifferenceEstimator::Subject* subject = 34 difference_estimator.MakeSubject(Region(kString2, sizeof(kString2))); 35 EXPECT_EQ(10U, difference_estimator.Measure(base, subject)); 36 } 37 38 TEST(DifferenceEstimatorTest, TestDifferentSuperstring) { 39 static const char kString1[] = "abcdabcdabcd"; 40 static const char kString2[] = "abcdabcdabcdabcd"; 41 DifferenceEstimator difference_estimator; 42 DifferenceEstimator::Base* base = 43 difference_estimator.MakeBase(Region(kString1, sizeof(kString1)-1)); 44 DifferenceEstimator::Subject* subject = 45 difference_estimator.MakeSubject(Region(kString2, sizeof(kString2)-1)); 46 EXPECT_EQ(1U, difference_estimator.Measure(base, subject)); 47 } 48 49 TEST(DifferenceEstimatorTest, TestDifferentSubstring) { 50 static const char kString1[] = "abcdabcdabcdabcd"; 51 static const char kString2[] = "abcdabcdabcd"; 52 DifferenceEstimator difference_estimator; 53 DifferenceEstimator::Base* base = 54 difference_estimator.MakeBase(Region(kString1, sizeof(kString1)-1)); 55 DifferenceEstimator::Subject* subject = 56 difference_estimator.MakeSubject(Region(kString2, sizeof(kString2)-1)); 57 EXPECT_EQ(1U, difference_estimator.Measure(base, subject)); 58 } 59