From 6122fd67a5bad939fbb7c4234d8c18d03d99c39f Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Thu, 7 Mar 2013 13:19:11 +0100 Subject: [PATCH] adg: improved AdgPoint handling Cleaned up API, refactored code, return a newly CpmlPair from adg_point_get_pair() and added test cases. --- src/adg/adg-adim.c | 77 +++++++++++--------- src/adg/adg-dim.c | 6 +- src/adg/adg-ldim.c | 36 ++++++---- src/adg/adg-point.c | 158 +++++++++++++++++++++++++---------------- src/adg/adg-point.h | 11 +-- src/adg/adg-rdim.c | 30 +++++--- src/adg/tests/.gitignore | 1 + src/adg/tests/Makefile.am | 4 ++ src/adg/tests/test-adim.c | 14 +--- src/adg/tests/test-dim.c | 21 +----- src/adg/tests/test-point.c | 171 +++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 372 insertions(+), 157 deletions(-) create mode 100644 src/adg/tests/test-point.c diff --git a/src/adg/adg-adim.c b/src/adg/adg-adim.c index 1e9f9d21..5f4ed982 100644 --- a/src/adg/adg-adim.c +++ b/src/adg/adg-adim.c @@ -309,11 +309,11 @@ adg_adim_new(void) /** * adg_adim_new_full: - * @ref1: allow-none: first reference point - * @ref2: allow-none: second reference point - * @org1: allow-none: first origin point - * @org2: allow-none: second origin point - * @pos: allow-none: the position point + * @ref1: (allow-none): first reference point + * @ref2: (allow-none): second reference point + * @org1: (allow-none): first origin point + * @org2: (allow-none): second origin point + * @pos: (allow-none): the position point * * Creates a new angular dimension, specifing all the needed * properties in one shot using #CpmlPair. @@ -361,8 +361,8 @@ adg_adim_new_full(const CpmlPair *ref1, const CpmlPair *ref2, * @org1_y: the y coordinate of start point of the first line * @org2_x: the x coordinate of start point of the second line * @org2_y: the y coordinate of start point of the second line - * @pos_x: the x coordinate of the position reference - * @pos_y: the y coordinate of the position reference + * @pos_x: the x coordinate of the position reference + * @pos_y: the y coordinate of the position reference * * Wrappes adg_adim_new_full() with explicit values. * @@ -395,12 +395,12 @@ adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y, /** * adg_adim_new_full_from_model: - * @model: transfer-none: the model from which the named pairs are taken - * @ref1: allow-none: the end point of the first line - * @ref2: allow-none: the end point of the second line - * @org1: allow-none: the origin of the first line - * @org2: allow-none: the origin of the second line - * @pos: allow-none: the position reference + * @model: (transfer none): the model from which the named pairs are taken + * @ref1: (allow-none): the end point of the first line + * @ref2: (allow-none): the end point of the second line + * @org1: (allow-none): the origin of the first line + * @org2: (allow-none): the origin of the second line + * @pos: (allow-none): the position reference * * Creates a new angular dimension, specifing all the needed properties * in one shot and using named pairs from @model. @@ -537,11 +537,9 @@ adg_adim_set_org1_from_model(AdgADim *adim, AdgModel *model, const gchar *org1) * @adim: an #AdgADim * * Gets the #AdgADim:org1 point. The returned point is internally owned - * and must not be freed or modified. Anyway, it is not const because - * adg_point_get_pair() must be able to modify the internal cache of - * the returned point. + * and must not be freed or modified. * - * Returns: the first reference point + * Returns: (transfer none): the first reference point * * Since: 1.0 **/ @@ -655,11 +653,9 @@ adg_adim_set_org2_from_model(AdgADim *adim, AdgModel *model, const gchar *org2) * @adim: an #AdgADim * * Gets the #AdgADim:org2 point. The returned point is internally owned - * and must not be freed or modified. Anyway, it is not const because - * adg_point_get_pair() must be able to modify the internal cache of - * the returned point. + * and must not be freed or modified. * - * Returns: the first reference point + * Returns: (transfer none): the second reference point * * Since: 1.0 **/ @@ -823,7 +819,7 @@ _adg_arrange(AdgEntity *entity) adim = (AdgADim *) entity; - if (!_adg_update_geometry(adim)) + if (! _adg_update_geometry(adim)) return; dim = (AdgDim *) adim; @@ -842,8 +838,8 @@ _adg_arrange(AdgEntity *entity) local = adg_entity_get_local_matrix(entity); extents.is_defined = FALSE; - cpml_pair_copy(&ref1, adg_point_get_pair(adg_dim_get_ref1(dim))); - cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim))); + cpml_pair_copy(&ref1, (CpmlPair *) adg_dim_get_ref1(dim)); + cpml_pair_copy(&ref2, (CpmlPair *) adg_dim_get_ref2(dim)); cpml_pair_copy(&base1, &data->point.base1); cpml_pair_copy(&base12, &data->point.base12); cpml_pair_copy(&base2, &data->point.base2); @@ -995,7 +991,7 @@ _adg_default_value(AdgDim *dim) dim_style = _ADG_GET_DIM_STYLE(dim); format = adg_dim_style_get_number_format(dim_style); - if (!_adg_update_geometry(adim)) + if (! _adg_update_geometry(adim)) return g_strdup("undef"); angle = (data->angle2 - data->angle1) * 180 / G_PI; @@ -1018,9 +1014,11 @@ _adg_update_geometry(AdgADim *adim) data = adim->data; + /* Check for cached results */ if (data->geometry_arranged) return TRUE; - else if (!_adg_get_info(adim, vector, ¢er, &distance)) + + if (! _adg_get_info(adim, vector, ¢er, &distance)) return FALSE; dim_style = _ADG_GET_DIM_STYLE(adim); @@ -1149,21 +1147,32 @@ _adg_get_info(AdgADim *adim, CpmlVector vector[], { AdgDim *dim; AdgADimPrivate *data; + AdgPoint *ref1_point, *ref2_point, *pos_point; const CpmlPair *ref1, *ref2, *pos; const CpmlPair *org1, *org2; gdouble factor; dim = (AdgDim *) adim; data = adim->data; - ref1 = adg_point_get_pair(adg_dim_get_ref1(dim)); - ref2 = adg_point_get_pair(adg_dim_get_ref2(dim)); - pos = adg_point_get_pair(adg_dim_get_pos(dim)); - org1 = adg_point_get_pair(data->org1); - org2 = adg_point_get_pair(data->org2); - - if (ref1 == NULL || ref2 == NULL || pos == NULL || - org1 == NULL || org2 == NULL) + ref1_point = adg_dim_get_ref1(dim); + ref2_point = adg_dim_get_ref2(dim); + pos_point = adg_dim_get_pos(dim); + + /* Check if the needed points are all properly defined */ + if (! adg_point_update(ref1_point) || + ! adg_point_update(ref2_point) || + ! adg_point_update(pos_point) || + ! adg_point_update(data->org1) || + ! adg_point_update(data->org2)) + { return FALSE; + } + + ref1 = (CpmlPair *) ref1_point; + ref2 = (CpmlPair *) ref2_point; + pos = (CpmlPair *) pos_point; + org1 = (CpmlPair *) data->org1; + org2 = (CpmlPair *) data->org2; vector[0].x = ref1->x - org1->x; vector[0].y = ref1->y - org1->y; diff --git a/src/adg/adg-dim.c b/src/adg/adg-dim.c index c045bc6d..c2aa0314 100644 --- a/src/adg/adg-dim.c +++ b/src/adg/adg-dim.c @@ -526,7 +526,7 @@ adg_dim_set_ref1_from_model(AdgDim *dim, AdgModel *model, const gchar *ref1) * * The returned point is internally owned and must not be freed * or modified. Anyway it is not const because a call to - * adg_point_get_pair() with the returned value must be able to + * adg_point_update() with the returned value must be able to * modify the internal cache. * * Returns: (transfer none): the first reference point. @@ -646,7 +646,7 @@ adg_dim_set_ref2_from_model(AdgDim *dim, AdgModel *model, const gchar *ref2) * * The returned point is internally owned and must not be freed * or modified. Anyway it is not const because a call to - * adg_point_get_pair() with the returned value must be able to + * adg_point_update() with the returned value must be able to * modify the internal cache. * * Returns: (transfer none): the second reference point. @@ -766,7 +766,7 @@ adg_dim_set_pos_from_model(AdgDim *dim, AdgModel *model, const gchar *pos) * * The returned point is internally owned and must not be freed * or modified. Anyway it is not const because a call to - * adg_point_get_pair() with the returned value must be able to + * adg_point_update() with the returned value must be able to * modify the internal cache. * * Returns: (transfer none): the position point. diff --git a/src/adg/adg-ldim.c b/src/adg/adg-ldim.c index 852fe533..06ab67d3 100644 --- a/src/adg/adg-ldim.c +++ b/src/adg/adg-ldim.c @@ -589,9 +589,9 @@ _adg_arrange(AdgEntity *entity) dim_style = _ADG_GET_DIM_STYLE(dim); local = adg_entity_get_local_matrix(entity); - cpml_pair_copy(&ref1, adg_point_get_pair(adg_dim_get_ref1(dim))); - cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim))); - cpml_pair_copy(&pos, adg_point_get_pair(adg_dim_get_pos(dim))); + cpml_pair_copy(&ref1, (CpmlPair *) adg_dim_get_ref1(dim)); + cpml_pair_copy(&ref2, (CpmlPair *) adg_dim_get_ref2(dim)); + cpml_pair_copy(&pos, (CpmlPair *) adg_dim_get_pos(dim)); cpml_pair_copy(&base1, &data->geometry.base1); cpml_pair_copy(&base2, &data->geometry.base2); @@ -749,25 +749,35 @@ _adg_update_geometry(AdgLDim *ldim) { AdgLDimPrivate *data; AdgDim *dim; - const CpmlPair *ref1, *ref2; - const CpmlPair *pos; + AdgPoint *ref1_point, *ref2_point, *pos_point; + const CpmlPair *ref1, *ref2, *pos; CpmlVector baseline, extension; gdouble d, k; data = ldim->data; - dim = (AdgDim *) ldim; - ref1 = adg_point_get_pair(adg_dim_get_ref1(dim)); - ref2 = adg_point_get_pair(adg_dim_get_ref2(dim)); - pos = adg_point_get_pair(adg_dim_get_pos(dim)); - /* Check if the needed pairs are properly defined */ - if (ref1 == NULL || ref2 == NULL || pos == NULL) { + /* Check for cached results */ + if (data->geometry.is_arranged) + return TRUE; + + dim = (AdgDim *) ldim; + ref1_point = adg_dim_get_ref1(dim); + ref2_point = adg_dim_get_ref2(dim); + pos_point = adg_dim_get_pos(dim); + + /* Check if the needed points are all properly defined */ + if (! adg_point_update(ref1_point) || + ! adg_point_update(ref2_point) || + ! adg_point_update(pos_point)) + { data->geometry.is_arranged = FALSE; return FALSE; - } else if (data->geometry.is_arranged) { - return TRUE; } + ref1 = (CpmlPair *) ref1_point; + ref2 = (CpmlPair *) ref2_point; + pos = (CpmlPair *) pos_point; + cpml_vector_from_angle(&extension, data->direction); cpml_pair_copy(&baseline, &extension); cpml_vector_normal(&baseline); diff --git a/src/adg/adg-point.c b/src/adg/adg-point.c index 8527d79d..d7462039 100644 --- a/src/adg/adg-point.c +++ b/src/adg/adg-point.c @@ -55,7 +55,7 @@ struct _AdgPoint { CpmlPair pair; AdgModel *model; gchar *name; - gboolean is_uptodate; + gboolean up_to_date; }; @@ -206,7 +206,7 @@ adg_point_set_pair_explicit(AdgPoint *point, gdouble x, gdouble y) adg_point_unset(point); point->pair.x = x; point->pair.y = y; - point->is_uptodate = TRUE; + point->up_to_date = TRUE; } /** @@ -243,20 +243,39 @@ adg_point_set_pair_from_model(AdgPoint *point, } /* Set the new named pair */ - point->is_uptodate = FALSE; + point->up_to_date = FALSE; point->model = model; point->name = g_strdup(name); } /** + * adg_point_invalidate: + * @point: an #AdgPoint + * + * Invalidates @point, forcing a refresh of its internal #CpmlPair if + * the point is linked to a named pair. If @point is explicitely set, + * this function has no effect. + * + * Since: 1.0 + **/ +void +adg_point_invalidate(AdgPoint *point) +{ + g_return_if_fail(point != NULL); + + if (point->model != NULL) + point->up_to_date = FALSE; +} + +/** * adg_point_unset: * @point: a pointer to an #AdgPoint * - * Unsets @point by resetting the internal "is_uptodate" flag. - * This also means @point is unlinked from the model if it is - * a named pair. In any cases, after this call the content of - * @point is undefined, that is calling adg_point_get_pair() - * will return %NULL. + * Unsets @point by resetting the internal %up_to_date flag and + * (eventually) unlinking it from the named pair it is bound to. + * After this call the content of @point is undefined, so a + * subsequent call to adg_point_get_pair() will return %NULL + * raising a warning. * * Since: 1.0 **/ @@ -271,52 +290,81 @@ adg_point_unset(AdgPoint *point) g_free(point->name); } - point->is_uptodate = FALSE; + point->up_to_date = FALSE; point->model = NULL; point->name = NULL; } /** + * adg_point_update: + * @point: a pointer to an #AdgPoint + * + * Updates the internal #CpmlPair of @point. The internal + * implementation is protected against multiple calls so it + * can be called more times without harms. + * + * Returns: %TRUE if @point has been updated or %FALSE on errors, + * i.e. when it is bound to a non-existent named pair. + * + * Since: 1.0 + **/ +gboolean +adg_point_update(AdgPoint *point) +{ + AdgModel *model; + const CpmlPair *pair; + + g_return_val_if_fail(point != NULL, FALSE); + + if (point->up_to_date) + return TRUE; + + model = point->model; + if (model == NULL) { + /* A point with explicit coordinates not up to date + * is an unexpected condition */ + g_warning(_("%s: trying to get a pair from an undefined point"), + G_STRLOC); + return FALSE; + } + + pair = adg_model_get_named_pair(model, point->name); + if (pair == NULL) + return FALSE; + + cpml_pair_copy(&point->pair, pair); + point->up_to_date = TRUE; + return TRUE; +} + +/** * adg_point_get_pair: * @point: an #AdgPoint * * #AdgPoint is an evolution of the pair concept but internally the * relevant data is still stored in an #CpmlPair struct. This function - * gets the pointer to this struct, updating the value prior to - * return it if needed (that is, if @point is linked to a not up - * to date named pair). + * returns a copy of the internally owned pair. + * + * + * The #CpmlPair is the first field of an #AdgPoint struct so casting + * is allowed between them and, in fact, it is often more convenient + * than calling this function. Just remember to update the internal + * pair by using adg_point_update() before. + * * - * Returns: the pair of @point or %NULL if the named pair does not exist + * Returns: (transfer full): the pair of @point or %NULL if the named pair does not exist * * Since: 1.0 **/ -const CpmlPair * +CpmlPair * adg_point_get_pair(AdgPoint *point) { g_return_val_if_fail(point != NULL, NULL); - if (!point->is_uptodate) { - const CpmlPair *pair; + if (! adg_point_update(point)) + return NULL; - if (point->model == NULL) { - /* A point with explicit coordinates not up to date - * is an unexpected condition */ - g_warning(_("%s: trying to get a pair from an undefined point"), - G_STRLOC); - return NULL; - } - - pair = adg_model_get_named_pair(point->model, point->name); - - /* "Named pair not found" condition: return NULL without warnings */ - if (pair == NULL) - return NULL; - - cpml_pair_copy(&point->pair, pair); - point->is_uptodate = TRUE; - } - - return (CpmlPair *) point; + return cpml_pair_dup(& point->pair); } /** @@ -332,7 +380,7 @@ adg_point_get_pair(AdgPoint *point) * Since: 1.0 **/ AdgModel * -adg_point_get_model(AdgPoint *point) +adg_point_get_model(const AdgPoint *point) { g_return_val_if_fail(point != NULL, NULL); return point->model; @@ -351,46 +399,32 @@ adg_point_get_model(AdgPoint *point) * Since: 1.0 **/ const gchar * -adg_point_get_name(AdgPoint *point) +adg_point_get_name(const AdgPoint *point) { g_return_val_if_fail(point != NULL, NULL); return point->name; } /** - * adg_point_invalidate: - * @point: an #AdgPoint - * - * Invalidates @point, forcing a refresh of its internal #CpmlPair if - * the point is linked to a named pair. If @point is explicitely set, - * this function has no effect. - * - * Since: 1.0 - **/ -void -adg_point_invalidate(AdgPoint *point) -{ - g_return_if_fail(point != NULL); - - if (point->model) - point->is_uptodate = FALSE; -} - -/** * adg_point_equal: * @point1: the first point to compare * @point2: the second point to compare * * Compares @point1 and @point2 and returns %TRUE if the points are - * equals. The comparison is made by matching also where the points - * are bound. If you want to compare only the coordinates, use - * cpml_pair_equal() directly on their pairs: + * equals. The comparison is made by checking also the named pairs + * they are bound to. If you want to compare only their coordinates, + * use cpml_pair_equal() directly on the #AdgPoint structs: * * |[ - * cpml_pair_equal(adg_point_get_pair(point1), adg_point_get_pair(point2)); + * if (adg_point_update(point1) && + * adg_point_update(point2) && + * cpml_pair_equal((CpmlPair *) point1, (CpmlPair *) point2)) + * { + * ... + * } * ]| * - * %NULL values are handled gracefully. + * %NULL points are handled gracefully. * * Returns: %TRUE if @point1 is equal to @point2, %FALSE otherwise * @@ -411,7 +445,7 @@ adg_point_equal(const AdgPoint *point1, const AdgPoint *point2) return FALSE; /* Handle points bound to named pairs */ - if (point1->model) + if (point1->model != NULL) return g_strcmp0(point1->name, point2->name) == 0; /* Handle points with explicit coordinates */ diff --git a/src/adg/adg-point.h b/src/adg/adg-point.h index b24ffe12..f21d85d0 100644 --- a/src/adg/adg-point.h +++ b/src/adg/adg-point.h @@ -29,7 +29,7 @@ G_BEGIN_DECLS -#define ADG_TYPE_POINT (adg_point_get_type()) +#define ADG_TYPE_POINT (adg_point_get_type()) GType adg_point_get_type (void) G_GNUC_CONST; @@ -47,11 +47,12 @@ void adg_point_set_pair_explicit (AdgPoint *point, void adg_point_set_pair_from_model (AdgPoint *point, AdgModel *model, const gchar *name); -void adg_point_unset (AdgPoint *point); -const CpmlPair *adg_point_get_pair (AdgPoint *point); -AdgModel * adg_point_get_model (AdgPoint *point); -const gchar * adg_point_get_name (AdgPoint *point); void adg_point_invalidate (AdgPoint *point); +void adg_point_unset (AdgPoint *point); +gboolean adg_point_update (AdgPoint *point); +CpmlPair * adg_point_get_pair (AdgPoint *point); +AdgModel * adg_point_get_model (const AdgPoint *point); +const gchar * adg_point_get_name (const AdgPoint *point); gboolean adg_point_equal (const AdgPoint *point1, const AdgPoint *point2); diff --git a/src/adg/adg-rdim.c b/src/adg/adg-rdim.c index 4f831a8b..b022e31a 100644 --- a/src/adg/adg-rdim.c +++ b/src/adg/adg-rdim.c @@ -391,7 +391,7 @@ _adg_arrange(AdgEntity *entity) quote = adg_dim_get_quote(dim); quote_entity = (AdgEntity *) quote; - if (!_adg_update_geometry(rdim)) + if (! _adg_update_geometry(rdim)) return; _adg_update_entities(rdim); @@ -410,7 +410,7 @@ _adg_arrange(AdgEntity *entity) local = adg_entity_get_local_matrix(entity); extents.is_defined = FALSE; - cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim))); + cpml_pair_copy(&ref2, (CpmlPair *) adg_dim_get_ref2(dim)); cpml_pair_copy(&base, &data->point.base); cpml_pair_transform(&ref2, local); @@ -537,7 +537,7 @@ _adg_default_value(AdgDim *dim) dim_style = _ADG_GET_DIM_STYLE(dim); format = adg_dim_style_get_number_format(dim_style); - if (!_adg_update_geometry(rdim)) + if (! _adg_update_geometry(rdim)) return g_strdup("undef"); return g_strdup_printf(format, data->radius); @@ -549,23 +549,33 @@ _adg_update_geometry(AdgRDim *rdim) AdgRDimPrivate *data; AdgDim *dim; AdgDimStyle *dim_style; - const CpmlPair *ref1, *ref2; - const CpmlPair *pos; + AdgPoint *ref1_point, *ref2_point, *pos_point; + const CpmlPair *ref1, *ref2, *pos; gdouble spacing, level, pos_distance; CpmlVector vector; data = rdim->data; + /* Check for cached results */ if (data->geometry_arranged) return TRUE; dim = (AdgDim *) rdim; - ref1 = adg_point_get_pair(adg_dim_get_ref1(dim)); - ref2 = adg_point_get_pair(adg_dim_get_ref2(dim)); - pos = adg_point_get_pair(adg_dim_get_pos(dim)); - - if (ref1 == NULL || ref2 == NULL || pos == NULL) + ref1_point = adg_dim_get_ref1(dim); + ref2_point = adg_dim_get_ref2(dim); + pos_point = adg_dim_get_pos(dim); + + /* Check if the needed points are all properly defined */ + if (! adg_point_update(ref1_point) || + ! adg_point_update(ref2_point) || + ! adg_point_update(pos_point)) + { return FALSE; + } + + ref1 = (CpmlPair *) ref1_point; + ref2 = (CpmlPair *) ref2_point; + pos = (CpmlPair *) pos_point; dim_style = _ADG_GET_DIM_STYLE(rdim); spacing = adg_dim_style_get_baseline_spacing(dim_style); diff --git a/src/adg/tests/.gitignore b/src/adg/tests/.gitignore index a397aedc..b5e27667 100644 --- a/src/adg/tests/.gitignore +++ b/src/adg/tests/.gitignore @@ -19,6 +19,7 @@ /test-logo /test-marker /test-model +/test-point /test-projection /test-rdim /test-stroke diff --git a/src/adg/tests/Makefile.am b/src/adg/tests/Makefile.am index 7ce7aafa..e961050e 100644 --- a/src/adg/tests/Makefile.am +++ b/src/adg/tests/Makefile.am @@ -23,6 +23,10 @@ TEST_PROGS+= test-model$(EXEEXT) test_model_SOURCES= test-model.c \ $(test_internals) +TEST_PROGS+= test-point$(EXEEXT) +test_point_SOURCES= test-point.c \ + $(test_internals) + TEST_PROGS+= test-trail$(EXEEXT) test_trail_SOURCES= test-trail.c \ $(test_internals) diff --git a/src/adg/tests/test-adim.c b/src/adg/tests/test-adim.c index 3de514bb..b88085a0 100644 --- a/src/adg/tests/test-adim.c +++ b/src/adg/tests/test-adim.c @@ -37,8 +37,7 @@ _adg_test_org1(void) adg_point_set_pair_explicit(origin, 0, 0); adg_point_set_pair_explicit(explicit_point, 123, 321); - adg_model_set_named_pair(model, "named-pair", - adg_point_get_pair(explicit_point)); + adg_model_set_named_pair(model, "named-pair", (CpmlPair *) explicit_point); adg_point_set_pair_from_model(model_point, model, "named-pair"); /* Ensure ADG does not consider an explicit point equals to @@ -61,11 +60,9 @@ _adg_test_org1(void) org1 = adg_adim_get_org1(adim); g_assert(adg_point_equal(org1, explicit_point)); - /* Checking the lazy assignment feature */ adg_adim_set_org1_from_model(adim, model, "dummy"); org1 = adg_adim_get_org1(adim); g_assert(org1 != NULL); - g_assert(adg_point_get_pair(org1) == NULL); adg_adim_set_org1_from_model(adim, model, "named-pair"); org1 = adg_adim_get_org1(adim); @@ -86,11 +83,9 @@ _adg_test_org1(void) g_assert(adg_point_equal(org1, explicit_point)); adg_point_destroy(org1); - /* Checking the lazy assignment feature */ adg_adim_set_org1_from_model(adim, model, "dummy"); g_object_get(adim, "org1", &org1, NULL); g_assert(org1 != NULL); - g_assert(adg_point_get_pair(org1) == NULL); adg_point_destroy(org1); g_object_set(adim, "org1", model_point, NULL); @@ -121,8 +116,7 @@ _adg_test_org2(void) adg_point_set_pair_explicit(origin, 0, 0); adg_point_set_pair_explicit(explicit_point, 123, 321); - adg_model_set_named_pair(model, "named-pair", - adg_point_get_pair(explicit_point)); + adg_model_set_named_pair(model, "named-pair", (CpmlPair *) explicit_point); adg_point_set_pair_from_model(model_point, model, "named-pair"); /* Ensure ADG does not consider an explicit point equals to @@ -145,11 +139,9 @@ _adg_test_org2(void) org2 = adg_adim_get_org2(adim); g_assert(adg_point_equal(org2, explicit_point)); - /* Checking the lazy assignment feature */ adg_adim_set_org2_from_model(adim, model, "dummy"); org2 = adg_adim_get_org2(adim); g_assert(org2 != NULL); - g_assert(adg_point_get_pair(org2) == NULL); adg_adim_set_org2_from_model(adim, model, "named-pair"); org2 = adg_adim_get_org2(adim); @@ -170,11 +162,9 @@ _adg_test_org2(void) g_assert(adg_point_equal(org2, explicit_point)); adg_point_destroy(org2); - /* Checking the lazy assignment feature */ adg_adim_set_org2_from_model(adim, model, "dummy"); g_object_get(adim, "org2", &org2, NULL); g_assert(org2 != NULL); - g_assert(adg_point_get_pair(org2) == NULL); adg_point_destroy(org2); g_object_set(adim, "org2", model_point, NULL); diff --git a/src/adg/tests/test-dim.c b/src/adg/tests/test-dim.c index 5d7caad0..8028d359 100644 --- a/src/adg/tests/test-dim.c +++ b/src/adg/tests/test-dim.c @@ -269,8 +269,7 @@ _adg_test_pos(void) adg_point_set_pair_explicit(origin, 0, 0); adg_point_set_pair_explicit(explicit_point, 123, 321); - adg_model_set_named_pair(model, "named-pair", - adg_point_get_pair(explicit_point)); + adg_model_set_named_pair(model, "named-pair", (CpmlPair *) explicit_point); adg_point_set_pair_from_model(model_point, model, "named-pair"); /* Ensure ADG does not consider an explicit point equals to @@ -293,11 +292,9 @@ _adg_test_pos(void) pos = adg_dim_get_pos(dim); g_assert(adg_point_equal(pos, explicit_point)); - /* Checking the lazy assignment feature */ adg_dim_set_pos_from_model(dim, model, "dummy"); pos = adg_dim_get_pos(dim); g_assert(pos != NULL); - g_assert(adg_point_get_pair(pos) == NULL); adg_dim_set_pos_from_model(dim, model, "named-pair"); pos = adg_dim_get_pos(dim); @@ -318,11 +315,9 @@ _adg_test_pos(void) g_assert(adg_point_equal(pos, explicit_point)); adg_point_destroy(pos); - /* Checking the lazy assignment feature */ adg_dim_set_pos_from_model(dim, model, "dummy"); g_object_get(dim, "pos", &pos, NULL); g_assert(pos != NULL); - g_assert(adg_point_get_pair(pos) == NULL); adg_point_destroy(pos); g_object_set(dim, "pos", model_point, NULL); @@ -353,8 +348,7 @@ _adg_test_ref1(void) adg_point_set_pair_explicit(origin, 0, 0); adg_point_set_pair_explicit(explicit_point, 123, 321); - adg_model_set_named_pair(model, "named-pair", - adg_point_get_pair(explicit_point)); + adg_model_set_named_pair(model, "named-pair", (CpmlPair *) explicit_point); adg_point_set_pair_from_model(model_point, model, "named-pair"); /* Ensure ADG does not consider an explicit point equals to @@ -377,11 +371,9 @@ _adg_test_ref1(void) ref1 = adg_dim_get_ref1(dim); g_assert(adg_point_equal(ref1, explicit_point)); - /* Checking the lazy assignment feature */ adg_dim_set_ref1_from_model(dim, model, "dummy"); ref1 = adg_dim_get_ref1(dim); g_assert(ref1 != NULL); - g_assert(adg_point_get_pair(ref1) == NULL); adg_dim_set_ref1_from_model(dim, model, "named-pair"); ref1 = adg_dim_get_ref1(dim); @@ -402,11 +394,9 @@ _adg_test_ref1(void) g_assert(adg_point_equal(ref1, explicit_point)); adg_point_destroy(ref1); - /* Checking the lazy assignment feature */ adg_dim_set_ref1_from_model(dim, model, "dummy"); g_object_get(dim, "ref1", &ref1, NULL); g_assert(ref1 != NULL); - g_assert(adg_point_get_pair(ref1) == NULL); adg_point_destroy(ref1); g_object_set(dim, "ref1", model_point, NULL); @@ -437,8 +427,7 @@ _adg_test_ref2(void) adg_point_set_pair_explicit(origin, 0, 0); adg_point_set_pair_explicit(explicit_point, 123, 321); - adg_model_set_named_pair(model, "named-pair", - adg_point_get_pair(explicit_point)); + adg_model_set_named_pair(model, "named-pair", (CpmlPair *) explicit_point); adg_point_set_pair_from_model(model_point, model, "named-pair"); /* Ensure ADG does not consider an explicit point equals to @@ -461,11 +450,9 @@ _adg_test_ref2(void) ref2 = adg_dim_get_ref2(dim); g_assert(adg_point_equal(ref2, explicit_point)); - /* Checking the lazy assignment feature */ adg_dim_set_ref2_from_model(dim, model, "dummy"); ref2 = adg_dim_get_ref2(dim); g_assert(ref2 != NULL); - g_assert(adg_point_get_pair(ref2) == NULL); adg_dim_set_ref2_from_model(dim, model, "named-pair"); ref2 = adg_dim_get_ref2(dim); @@ -486,11 +473,9 @@ _adg_test_ref2(void) g_assert(adg_point_equal(ref2, explicit_point)); adg_point_destroy(ref2); - /* Checking the lazy assignment feature */ adg_dim_set_ref2_from_model(dim, model, "dummy"); g_object_get(dim, "ref2", &ref2, NULL); g_assert(ref2 != NULL); - g_assert(adg_point_get_pair(ref2) == NULL); adg_point_destroy(ref2); g_object_set(dim, "ref2", model_point, NULL); diff --git a/src/adg/tests/test-point.c b/src/adg/tests/test-point.c new file mode 100644 index 00000000..3da0d874 --- /dev/null +++ b/src/adg/tests/test-point.c @@ -0,0 +1,171 @@ +/* ADG - Automatic Drawing Generation + * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Nicola Fontana + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#include "test-internal.h" + + +static void +_adg_test_generic(void) +{ + AdgPoint *point, *dup_point; + CpmlPair *pair, *dup_pair; + CpmlPair dummy_pair = { 3.4, 5.6 }; + + point = adg_point_new(); + g_assert(point != NULL); + + adg_point_set_pair_explicit(point, 1, 2); + pair = (CpmlPair *) point; + g_assert_cmpfloat(pair->x, ==, 1); + g_assert_cmpfloat(pair->y, ==, 2); + + pair = adg_point_get_pair(point); + g_assert(pair != NULL); + g_assert_cmpfloat(pair->x, ==, 1); + g_assert_cmpfloat(pair->y, ==, 2); + + dup_point = adg_point_dup(point); + g_assert(dup_point != NULL); + g_assert(dup_point != point); + + /* Should be a noop with explicit pairs */ + adg_point_invalidate(point); + + dup_pair = adg_point_get_pair(dup_point); + g_assert(dup_pair != NULL); + g_assert(dup_pair != pair); + + g_assert_cmpfloat(pair->x, ==, dup_pair->x); + g_assert_cmpfloat(pair->y, ==, dup_pair->y); + g_assert(adg_point_equal(point, dup_point)); + + g_free(dup_pair); + adg_point_destroy(dup_point); + + dup_point = adg_point_new(); + adg_point_set_pair(dup_point, &dummy_pair); + dup_pair = (CpmlPair *) dup_point; + + /* Should be a noop with explicit pairs */ + adg_point_invalidate(dup_point); + + g_assert_cmpfloat(dup_pair->x, ==, 3.4); + g_assert_cmpfloat(dup_pair->y, ==, 5.6); + g_assert(! adg_point_equal(point, dup_point)); + + adg_point_copy(dup_point, point); + dup_pair = adg_point_get_pair(dup_point); + g_assert(dup_pair != NULL); + g_assert(dup_pair != pair); + + g_assert_cmpfloat(pair->x, ==, dup_pair->x); + g_assert_cmpfloat(pair->y, ==, dup_pair->y); + g_assert(adg_point_equal(point, dup_point)); + + g_free(pair); + g_free(dup_pair); + adg_point_destroy(point); + adg_point_destroy(dup_point); +} + +static void +_adg_test_named_pair(void) +{ + CpmlPair p1 = { 123, 456 }; + AdgPoint *explicit_point, *explicit_point2, *model_point; + AdgModel *model; + CpmlPair *pair; + + explicit_point = adg_point_new(); + g_assert(explicit_point != NULL); + adg_point_set_pair(explicit_point, &p1); + + explicit_point2 = adg_point_new(); + g_assert(explicit_point2 != NULL); + adg_point_set_pair_explicit(explicit_point2, p1.x, p1.y); + + /* Checking comparison APIs */ + g_assert(adg_point_equal(explicit_point, explicit_point2)); + adg_point_set_pair_explicit(explicit_point2, 78, 90); + g_assert(! adg_point_equal(explicit_point, explicit_point2)); + pair = (CpmlPair *) explicit_point2; + g_assert_cmpfloat(pair->x, ==, 78); + g_assert_cmpfloat(pair->y, ==, 90); + + pair = adg_point_get_pair(explicit_point); + g_assert(cpml_pair_equal(pair, &p1)); + g_free(pair); + + model = ADG_MODEL(adg_path_new()); + g_assert(model != NULL); + adg_model_set_named_pair(model, "named-pair", &p1); + + model_point = adg_point_new(); + g_assert(model_point != NULL); + adg_point_set_pair_from_model(model_point, model, "named-pair"); + + pair = adg_point_get_pair(model_point); + g_assert(cpml_pair_equal(pair, &p1)); + g_free(pair); + + /* Ensure ADG does not consider an explicit point equals to + * a point bound to a named pair with the same coordinates */ + g_assert(! adg_point_equal(explicit_point, model_point)); + + /* Check for lazy evaluation of named pairs */ + adg_point_set_pair_from_model(model_point, model, "unexistent-pair"); + pair = (CpmlPair *) model_point; + g_assert_cmpfloat(pair->x, ==, p1.x); + g_assert_cmpfloat(pair->y, ==, p1.y); + + /* Check behavior on undefined named pair */ + g_assert(! adg_point_update(model_point)); + g_assert(adg_point_get_pair(model_point) == NULL); + + adg_point_set_pair_from_model(model_point, model, "named-pair"); + g_assert(adg_point_update(model_point)); + + /* Check for case sensitiveness */ + adg_point_set_pair_from_model(model_point, model, "Named-Pair"); + g_assert(adg_point_get_pair(model_point) == NULL); + g_assert(! adg_point_update(model_point)); + + /* Check if adg_point_get_pair() triggers an adg_point_update() */ + adg_point_set_pair_from_model(model_point, model, "named-pair"); + pair = adg_point_get_pair(model_point); + g_assert(cpml_pair_equal(pair, &p1)); + g_free(pair); + + adg_point_destroy(explicit_point); + adg_point_destroy(model_point); + g_object_unref(model); +} + + +int +main(int argc, char *argv[]) +{ + adg_test_init(&argc, &argv); + + adg_test_add_func("/adg/point/generic", _adg_test_generic); + adg_test_add_func("/adg/point/named-pair", _adg_test_named_pair); + + return g_test_run(); +} -- 2.11.4.GIT