clip_and_split.c (4764B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "common/rect.h" 21 22#include <CUnit/CUnit.h> 23 24/** 25 * Test which verifies that guac_common_rect_clip_and_split() divides a 26 * rectangle into subrectangles after removing a "hole" rectangle. 27 */ 28void test_rect__clip_and_split() { 29 30 int res; 31 32 guac_common_rect cut; 33 guac_common_rect min; 34 guac_common_rect rect; 35 36 guac_common_rect_init(&min, 10, 10, 10, 10); 37 38 /* Clip top */ 39 guac_common_rect_init(&rect, 10, 5, 10, 10); 40 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 41 CU_ASSERT_EQUAL(1, res); 42 CU_ASSERT_EQUAL(10, cut.x); 43 CU_ASSERT_EQUAL(5, cut.y); 44 CU_ASSERT_EQUAL(10, cut.width); 45 CU_ASSERT_EQUAL(5, cut.height); 46 47 CU_ASSERT_EQUAL(10, rect.x); 48 CU_ASSERT_EQUAL(10, rect.y); 49 CU_ASSERT_EQUAL(10, rect.width); 50 CU_ASSERT_EQUAL(5, rect.height); 51 52 /* Clip bottom */ 53 guac_common_rect_init(&rect, 10, 15, 10, 10); 54 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 55 CU_ASSERT_EQUAL(1, res); 56 CU_ASSERT_EQUAL(10, cut.x); 57 CU_ASSERT_EQUAL(20, cut.y); 58 CU_ASSERT_EQUAL(10, cut.width); 59 CU_ASSERT_EQUAL(5, cut.height); 60 61 CU_ASSERT_EQUAL(10, rect.x); 62 CU_ASSERT_EQUAL(15, rect.y); 63 CU_ASSERT_EQUAL(10, rect.width); 64 CU_ASSERT_EQUAL(5, rect.height); 65 66 /* Clip left */ 67 guac_common_rect_init(&rect, 5, 10, 10, 10); 68 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 69 CU_ASSERT_EQUAL(1, res); 70 CU_ASSERT_EQUAL(5, cut.x); 71 CU_ASSERT_EQUAL(10, cut.y); 72 CU_ASSERT_EQUAL(5, cut.width); 73 CU_ASSERT_EQUAL(10, cut.height); 74 75 CU_ASSERT_EQUAL(10, rect.x); 76 CU_ASSERT_EQUAL(10, rect.y); 77 CU_ASSERT_EQUAL(5, rect.width); 78 CU_ASSERT_EQUAL(10, rect.height); 79 80 /* Clip right */ 81 guac_common_rect_init(&rect, 15, 10, 10, 10); 82 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 83 CU_ASSERT_EQUAL(1, res); 84 CU_ASSERT_EQUAL(20, cut.x); 85 CU_ASSERT_EQUAL(10, cut.y); 86 CU_ASSERT_EQUAL(5, cut.width); 87 CU_ASSERT_EQUAL(10, cut.height); 88 89 CU_ASSERT_EQUAL(15, rect.x); 90 CU_ASSERT_EQUAL(10, rect.y); 91 CU_ASSERT_EQUAL(5, rect.width); 92 CU_ASSERT_EQUAL(10, rect.height); 93 94 /* 95 * Test a rectangle which completely covers the hole. 96 * Clip and split until done. 97 */ 98 guac_common_rect_init(&rect, 5, 5, 20, 20); 99 100 /* Clip top */ 101 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 102 CU_ASSERT_EQUAL(1, res); 103 CU_ASSERT_EQUAL(5, cut.x); 104 CU_ASSERT_EQUAL(5, cut.y); 105 CU_ASSERT_EQUAL(20, cut.width); 106 CU_ASSERT_EQUAL(5, cut.height); 107 108 CU_ASSERT_EQUAL(5, rect.x); 109 CU_ASSERT_EQUAL(10, rect.y); 110 CU_ASSERT_EQUAL(20, rect.width); 111 CU_ASSERT_EQUAL(15, rect.height); 112 113 /* Clip left */ 114 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 115 CU_ASSERT_EQUAL(1, res); 116 CU_ASSERT_EQUAL(5, cut.x); 117 CU_ASSERT_EQUAL(10, cut.y); 118 CU_ASSERT_EQUAL(5, cut.width); 119 CU_ASSERT_EQUAL(15, cut.height); 120 121 CU_ASSERT_EQUAL(10, rect.x); 122 CU_ASSERT_EQUAL(10, rect.y); 123 CU_ASSERT_EQUAL(15, rect.width); 124 CU_ASSERT_EQUAL(15, rect.height); 125 126 /* Clip bottom */ 127 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 128 CU_ASSERT_EQUAL(1, res); 129 CU_ASSERT_EQUAL(10, cut.x); 130 CU_ASSERT_EQUAL(20, cut.y); 131 CU_ASSERT_EQUAL(15, cut.width); 132 CU_ASSERT_EQUAL(5, cut.height); 133 134 CU_ASSERT_EQUAL(10, rect.x); 135 CU_ASSERT_EQUAL(10, rect.y); 136 CU_ASSERT_EQUAL(15, rect.width); 137 CU_ASSERT_EQUAL(10, rect.height); 138 139 /* Clip right */ 140 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 141 CU_ASSERT_EQUAL(20, cut.x); 142 CU_ASSERT_EQUAL(10, cut.y); 143 CU_ASSERT_EQUAL(5, cut.width); 144 CU_ASSERT_EQUAL(10, cut.height); 145 146 CU_ASSERT_EQUAL(10, rect.x); 147 CU_ASSERT_EQUAL(10, rect.y); 148 CU_ASSERT_EQUAL(10, rect.width); 149 CU_ASSERT_EQUAL(10, rect.height); 150 151 /* Make sure nothing is left to do */ 152 res = guac_common_rect_clip_and_split(&rect, &min, &cut); 153 CU_ASSERT_EQUAL(0, res); 154 155} 156