[Nickle] nickle: Branch 'master' - 4 commits

Keith Packard keithp at keithp.com
Wed Aug 18 10:54:40 PDT 2021


 configure.ac     |    4 ++--
 debian/changelog |    7 +++++++
 prng.5c          |    6 ++++++
 sort.5c          |    2 +-
 test/Makefile.am |    3 ++-
 test/sorttest.5c |   39 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 57 insertions(+), 4 deletions(-)

New commits:
commit 502ae929a9a0bcd2ef986303427f2a407ec89f50
Author: Keith Packard <keithp at keithp.com>
Date:   Wed Aug 18 10:03:26 2021 -0700

    Version 2.91
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/configure.ac b/configure.ac
index 03e501f..2607ba3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,8 +6,8 @@ dnl for licensing information.
 
 AC_PREREQ([2.69])
 
-AC_INIT([nickle],[2.90],[http://nickle.org],[nickle])
-RELEASE_DATE="2020-12-19"
+AC_INIT([nickle],[2.91],[http://nickle.org],[nickle])
+RELEASE_DATE="2021-08-18"
 AC_CONFIG_SRCDIR([nickle.h])
 AC_CONFIG_HEADERS([nickle-config.h])
 AC_CONFIG_AUX_DIR(.)
diff --git a/debian/changelog b/debian/changelog
index f0bfce0..24bb0ad 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+nickle (2.91) unstable; urgency=medium
+
+  * Make randint produce evenly distributed values
+  * Use asprintf in Command::edit to avoid buffer overflow. Closes: #992413.
+
+ -- Keith Packard <keithp at keithp.com>  Wed, 18 Aug 2021 10:01:45 -0700
+
 nickle (2.90) unstable; urgency=medium
 
   * Fix a syntax error in sort.5c.
commit 0dbfdfcd2be2963ff3a3bf6a71d99716141440dc
Author: Keith Packard <keithp at keithp.com>
Date:   Wed Aug 18 10:49:29 2021 -0700

    test: Add test for sort functions
    
    Generate a random array of integers and make sure they end up being
    sorted correctly by both qsort and mergesort.
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/test/Makefile.am b/test/Makefile.am
index d7fe7fc..39dc364 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -16,7 +16,8 @@ check_SCRIPTS=\
 	is_type.5c \
 	jsontest.5c \
 	datetest.5c \
-	string-file.5c
+	string-file.5c \
+	sorttest.5c
 
 TABLES=math-tables.5c
 
diff --git a/test/sorttest.5c b/test/sorttest.5c
new file mode 100644
index 0000000..255d211
--- /dev/null
+++ b/test/sorttest.5c
@@ -0,0 +1,39 @@
+/* Make sure sorting works */
+
+autoimport PRNG;
+autoimport Sort;
+
+bool ordered(real[] values)
+{
+    int d = dim(values);
+    if (d == 0)
+	return true;
+    real a = values[0];
+    for (int i = 1; i < d; i++) {
+	if (a > values[i])
+	    return false;
+	a = values[i];
+    }
+    return true;
+}
+
+void check(&real[] values, string sort)
+{
+    if (ordered(values))
+	return;
+    abort("check failed %v not ordered by %s", values, sort);
+}
+
+bool int_gt(int a, int b) = a > b;
+
+for (int i = 0; i < 100; i++) {
+    int len = randint(10000);
+    printf("test %d len %d\n", i, len);
+    real[*] v = Sort::randomints(len, 1000000);
+    real[*] qv = v;
+    real[*] mv = v;
+    Sort::qsort(&qv, int_gt);
+    check(&qv, "qsort");
+    Sort::mergesort(&mv, int_gt);
+    check(&mv, "mergesort");
+}
commit fec36b59d24aedeb94c01d5becd302e9cd0702d0
Author: Keith Packard <keithp at keithp.com>
Date:   Wed Aug 18 10:48:47 2021 -0700

    prng: Make randint work for values <= 0
    
    randint(0) returns 0
    randint(-n) return -randint(-n)
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/prng.5c b/prng.5c
index fecf367..e19fe32 100644
--- a/prng.5c
+++ b/prng.5c
@@ -61,6 +61,12 @@ namespace PRNG {
      * Returns a random integer in the range 0..'n'-1 
      */
   {
+    if (n == 0)
+      return 0;
+
+    if (n < 0)
+      return -randint(-n);
+
     int bits = bit_width(n);
 
     for (;;) {
commit 1ed4a16f4b08fc17d07c7ac75fb8b39c54374d33
Author: Keith Packard <keithp at keithp.com>
Date:   Wed Aug 18 10:43:03 2021 -0700

    sort: make pivot computation pass positive value to randint
    
    The pivot computation was computing the wrong value to pass to
    randint, using the lower bound minus the upper bound. When randint was
    changed to make the distribution equal, passing negative values caused
    an infinite loop making this bug more obvious.
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/sort.5c b/sort.5c
index 56d3e5d..7844135 100644
--- a/sort.5c
+++ b/sort.5c
@@ -23,7 +23,7 @@ namespace Sort {
 		/* partition the array into two pieces and return the pivot */
 		int partition (int p, int r) {
 		    /* select a random element to pivot */
-		    int pivot = p + PRNG::randint(p-r);
+		    int pivot = p + PRNG::randint(r-p);
 		    exchange (pivot, r);
 		    
 		    poly x = a[r];


More information about the Nickle mailing list