From 6be12778a162063cbdcd0e79043bd5a4ef2204a6 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 1 Mar 2012 23:11:22 -0800 Subject: [PATCH] Sort attributes by name for display, to make order arch-independent. When data file attributes or variable attributes were displayed by e.g. DISPLAY ATTRIBUTES or SYSFILE INFO, they were sorted in hash order, which varies by architecture. Therefore, the tests that depended on this order failed on big-endian machines such as mips. This commit fixes the problem by displaying attributes in sorted order. This fixes a failure in the "Perl copying system files" reported by Debian buildds. --- src/data/attributes.c | 37 ++++++++++++++++++++++++- src/data/attributes.h | 4 +-- src/language/dictionary/sys-file-info.c | 12 +++++--- tests/data/sys-file-reader.at | 6 ++-- tests/perl-module.at | 8 +++--- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/data/attributes.c b/src/data/attributes.c index 7fc546ee23..d262e2ebc3 100644 --- a/src/data/attributes.c +++ b/src/data/attributes.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc. + Copyright (C) 2008, 2009, 2011, 2012 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -299,3 +299,38 @@ attrset_next (const struct attrset *set, struct attrset_iterator *iterator) iterator->node = hmap_next (&set->map, iterator->node); return iterator_data (iterator); } + +static int +compare_attribute_by_name (const void *a_, const void *b_) +{ + const struct attribute *const *a = a_; + const struct attribute *const *b = b_; + + return strcmp ((*a)->name, (*b)->name); +} + +/* Allocates and returns an array of pointers to attributes + that is sorted by attribute name. The array has + 'attrset_count (SET)' elements. The caller is responsible for + freeing the array. */ +struct attribute ** +attrset_sorted (const struct attrset *set) +{ + if (set != NULL && attrset_count (set) > 0) + { + struct attribute **attrs; + struct attribute *attr; + size_t i; + + attrs = xmalloc (attrset_count (set) * sizeof *attrs); + i = 0; + HMAP_FOR_EACH (attr, struct attribute, node, &set->map) + attrs[i++] = attr; + assert (i == attrset_count (set)); + qsort (attrs, attrset_count (set), sizeof *attrs, + compare_attribute_by_name); + return attrs; + } + else + return NULL; +} diff --git a/src/data/attributes.h b/src/data/attributes.h index 61b68f1160..ab7b12e301 100644 --- a/src/data/attributes.h +++ b/src/data/attributes.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008, 2011 Free Software Foundation, Inc. + Copyright (C) 2008, 2011, 2012 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -65,6 +65,6 @@ struct attribute *attrset_first (const struct attrset *, struct attrset_iterator *); struct attribute *attrset_next (const struct attrset *, struct attrset_iterator *); - +struct attribute **attrset_sorted (const struct attrset *); #endif /* data/attributes.h */ diff --git a/src/language/dictionary/sys-file-info.c b/src/language/dictionary/sys-file-info.c index 31a685aa82..441054e209 100644 --- a/src/language/dictionary/sys-file-info.c +++ b/src/language/dictionary/sys-file-info.c @@ -377,12 +377,15 @@ static void display_attributes (struct tab_table *t, const struct attrset *set, int flags, int c, int r) { - struct attrset_iterator i; - struct attribute *attr; + struct attribute **attrs; + size_t n_attrs; + size_t i; - for (attr = attrset_first (set, &i); attr != NULL; - attr = attrset_next (set, &i)) + n_attrs = attrset_count (set); + attrs = attrset_sorted (set); + for (i = 0; i < n_attrs; i++) { + const struct attribute *attr = attrs[i]; const char *name = attribute_get_name (attr); size_t n_values; size_t i; @@ -401,6 +404,7 @@ display_attributes (struct tab_table *t, const struct attrset *set, int flags, r++; } } + free (attrs); } static void diff --git a/tests/data/sys-file-reader.at b/tests/data/sys-file-reader.at index 37866161d9..9fd8c09e19 100644 --- a/tests/data/sys-file-reader.at +++ b/tests/data/sys-file-reader.at @@ -1088,18 +1088,18 @@ DISPLAY ATTRIBUTES. AT_CHECK([cat pspp.csv], [0], [[Variable,Description, FirstVariable,Custom attributes:, -,bert,123 ,adèle[1],23 ,adèle[2],34 +,bert,123 SécondVariable,Custom attributes:, ,xyzzy,quux Table: Custom data file attributes. Attribute,Value -SécondAttr[1],123 -SécondAttr[2],456 Attr1[1],Value1 Attr1[2],'déclaration' +SécondAttr[1],123 +SécondAttr[2],456 ]]) done AT_CLEANUP diff --git a/tests/perl-module.at b/tests/perl-module.at index b24278b7fc..1f4a96288c 100644 --- a/tests/perl-module.at +++ b/tests/perl-module.at @@ -481,11 +481,11 @@ numeric,A Numeric Variable,,3 ,2,Duality, ,3,Thripality, ,Custom attributes:,, -,size,large, -,nationality,foreign, ,colour[1],blue, ,colour[2],pink, ,colour[3],violet, +,nationality,foreign, +,size,large, date,A Date Variable,,4 ,Format: DATE11,, ,Measure: Scale,, @@ -535,11 +535,11 @@ numeric,A Numeric Variable,,3 ,2,Duality, ,3,Thripality, ,Custom attributes:,, -,size,large, -,nationality,foreign, ,colour[1],blue, ,colour[2],pink, ,colour[3],violet, +,nationality,foreign, +,size,large, date,A Date Variable,,4 ,Format: DATE11,, ,Measure: Scale,, -- 2.30.2