epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 8 | // Unit tests for src/core/SkPoint.cpp and its header |
| 9 | |
| 10 | #include "SkPoint.h" |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame^] | 11 | #include "SkRect.h" |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 12 | #include "Test.h" |
| 13 | |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame^] | 14 | static void test_casts(skiatest::Reporter* reporter) { |
| 15 | SkPoint p = { 0, 0 }; |
| 16 | SkRect r = { 0, 0, 0, 0 }; |
| 17 | |
| 18 | const SkScalar* pPtr = SkTCast<const SkScalar*>(&p); |
| 19 | const SkScalar* rPtr = SkTCast<const SkScalar*>(&r); |
| 20 | |
| 21 | REPORTER_ASSERT(reporter, p.asScalars() == pPtr); |
| 22 | REPORTER_ASSERT(reporter, r.asScalars() == rPtr); |
| 23 | } |
| 24 | |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 25 | // Tests that SkPoint::length() and SkPoint::Length() both return |
| 26 | // approximately expectedLength for this (x,y). |
| 27 | static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y, |
| 28 | SkScalar expectedLength) { |
| 29 | SkPoint point; |
| 30 | point.set(x, y); |
| 31 | SkScalar s1 = point.length(); |
| 32 | SkScalar s2 = SkPoint::Length(x, y); |
bungeman@google.com | b7b5d93 | 2012-08-24 19:53:58 +0000 | [diff] [blame] | 33 | //The following should be exactly the same, but need not be. |
borenet@google.com | 98a9e1f | 2012-08-27 13:14:46 +0000 | [diff] [blame] | 34 | //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323 |
bungeman@google.com | b7b5d93 | 2012-08-24 19:53:58 +0000 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2)); |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength)); |
| 37 | } |
| 38 | |
| 39 | // Tests SkPoint::Normalize() for this (x,y) |
| 40 | static void test_Normalize(skiatest::Reporter* reporter, |
| 41 | SkScalar x, SkScalar y) { |
| 42 | SkPoint point; |
| 43 | point.set(x, y); |
| 44 | SkScalar oldLength = point.length(); |
| 45 | SkScalar returned = SkPoint::Normalize(&point); |
| 46 | SkScalar newLength = point.length(); |
bungeman@google.com | b7b5d93 | 2012-08-24 19:53:58 +0000 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength)); |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1)); |
| 49 | } |
| 50 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 51 | static void PointTest(skiatest::Reporter* reporter) { |
reed@google.com | c7d0ea3 | 2013-03-08 16:07:54 +0000 | [diff] [blame^] | 52 | test_casts(reporter); |
| 53 | |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 54 | test_length(reporter, SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5)); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 55 | test_length(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f), |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 56 | SK_Scalar1); |
| 57 | test_Normalize(reporter, SkIntToScalar(3), SkIntToScalar(4)); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 58 | test_Normalize(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f)); |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | #include "TestClassDef.h" |
| 62 | DEFINE_TESTCLASS("Point", PointTestClass, PointTest) |