blob: b8f4398c207bd9efa401ab4dd4f3ec88d49991d5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com1fd56dc2011-06-15 18:04:58 +00008// Unit tests for src/core/SkPoint.cpp and its header
9
10#include "SkPoint.h"
reed@google.comc7d0ea32013-03-08 16:07:54 +000011#include "SkRect.h"
epoger@google.com1fd56dc2011-06-15 18:04:58 +000012#include "Test.h"
13
reed@google.comc7d0ea32013-03-08 16:07:54 +000014static 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.com1fd56dc2011-06-15 18:04:58 +000025// Tests that SkPoint::length() and SkPoint::Length() both return
26// approximately expectedLength for this (x,y).
27static 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.comb7b5d932012-08-24 19:53:58 +000033 //The following should be exactly the same, but need not be.
borenet@google.com98a9e1f2012-08-27 13:14:46 +000034 //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
bungeman@google.comb7b5d932012-08-24 19:53:58 +000035 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
epoger@google.com1fd56dc2011-06-15 18:04:58 +000036 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
37}
38
39// Tests SkPoint::Normalize() for this (x,y)
40static 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.comb7b5d932012-08-24 19:53:58 +000047 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
epoger@google.com1fd56dc2011-06-15 18:04:58 +000048 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
49}
50
caryclark@google.com42639cd2012-06-06 12:03:39 +000051static void PointTest(skiatest::Reporter* reporter) {
reed@google.comc7d0ea32013-03-08 16:07:54 +000052 test_casts(reporter);
53
epoger@google.com1fd56dc2011-06-15 18:04:58 +000054 test_length(reporter, SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5));
tomhudson@google.com75589252012-04-10 17:42:21 +000055 test_length(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f),
epoger@google.com1fd56dc2011-06-15 18:04:58 +000056 SK_Scalar1);
57 test_Normalize(reporter, SkIntToScalar(3), SkIntToScalar(4));
tomhudson@google.com75589252012-04-10 17:42:21 +000058 test_Normalize(reporter, SkFloatToScalar(0.6f), SkFloatToScalar(0.8f));
epoger@google.com1fd56dc2011-06-15 18:04:58 +000059}
60
61#include "TestClassDef.h"
62DEFINE_TESTCLASS("Point", PointTestClass, PointTest)