Index: trunk/psLib/test/mathtypes/.cvsignore
===================================================================
--- trunk/psLib/test/mathtypes/.cvsignore	(revision 18331)
+++ trunk/psLib/test/mathtypes/.cvsignore	(revision 18332)
@@ -21,2 +21,3 @@
 tap_psVectorSort
 tap_psVectorSortIndex
+tap_psVectorSelect
Index: trunk/psLib/test/mathtypes/Makefile.am
===================================================================
--- trunk/psLib/test/mathtypes/Makefile.am	(revision 18331)
+++ trunk/psLib/test/mathtypes/Makefile.am	(revision 18332)
@@ -18,5 +18,6 @@
 	tap_psVector \
 	tap_psVectorSort \
-	tap_psVectorSortIndex
+	tap_psVectorSortIndex \
+	tap_psVectorSelect
 
 if BUILD_TESTS
Index: trunk/psLib/test/mathtypes/tap_psVectorSelect.c
===================================================================
--- trunk/psLib/test/mathtypes/tap_psVectorSelect.c	(revision 18332)
+++ trunk/psLib/test/mathtypes/tap_psVectorSelect.c	(revision 18332)
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <pslib.h>
+#include "tap.h"
+#include "pstap.h"
+
+#define SIZE 1001                       // Size of vector
+#define NUM_NONZERO 50                  // Number of non-zero values
+
+// Test that results of psVectorSelect match what you'd get from psVectorSort
+// 2 tests
+static void testSelect(int size,        // Size of vector
+                       int numNonzero   // Number of non-zero values
+                       )
+{
+    psMemId id = psMemGetId();
+
+    psVector *vector = psVectorAlloc(size, PS_TYPE_F32);
+    psVectorInit(vector, 0.0);
+    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS, 12345);
+    for (long i = 0; i < numNonzero; i++) {
+        long index = psRandomUniform(rng) * size;
+        vector->data.F32[index] = psRandomUniform(rng);
+    }
+    psFree(rng);
+
+    psVector *sorted = psVectorSort(NULL, vector); // Sorted vector
+    psVector *select = NULL;        // Vector for selecting
+    bool match = true;              // Everything matches
+    for (long i = 0; i < size && match; i++) {
+        select = psVectorSelect(select, vector, i);
+        if (select->data.F32[i] != sorted->data.F32[i]) {
+            match = false;
+            diag("Rank %ld doesn't match: %f vs %f", i, select->data.F32[i], sorted->data.F32[i]);
+        }
+    }
+    psFree(sorted);
+    psFree(select);
+    psFree(vector);
+
+    ok(match, "All selections match, size=%d, number non-zero=%d.", size, numNonzero);
+    ok(!psMemCheckLeaks(id, NULL, NULL, false), "no memory leaks");
+}
+
+
+int main(int argc, char *argv[])
+{
+    plan_tests(5 * 2);
+
+    testSelect(100, 0);
+    testSelect(100, 10);
+    testSelect(100, 100);
+    testSelect(137, 13);
+    testSelect(137, 137);
+
+    return PS_EXIT_SUCCESS;
+}
