3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
11 # option 1: with wfpl-cms
15 # copy/rename this file to test_db_reposition.php in your main dir
17 # visit http://yourdomain.tld/test_db_reposition
19 # refresh to test more times
21 # drop the "test_db_reposition" table when you're done
25 # require inc/wfpl/db.php
26 # function message($msg) { print "$msg\n"; }
28 # test_db_reposition_main()
31 function test_db_reposition_main () {
33 'create table if not exists test_db_reposition ('
34 . ' id int unique auto_increment,'
35 . ' ord int(11) not null default 0'
38 db_delete('test_db_reposition');
40 for ($i = 0; $i < 10; ++$i) {
41 db_insert('test_db_reposition', 'ord', $i + 1);
42 $ids[] = db_auto_id();
46 for ($tests = 0; $tests < $max_tests; ++$tests) {
47 # decide which one to move, and to where
48 $to = $from = rand(0, 9);
49 while ($to === $from) {
50 if ($tests < $max_tests / 3) {
51 # full range (respace rare)
53 } elseif ($tests < $max_tests / 3 * 2) {
54 # never place first (respaces)
57 # never place last (respaces)
62 #message("from: $from (id " . $ids[$from] . "), to: $to");
65 $new_ord = db_reposition(
66 'test_db_reposition', # table
67 $ids[$from], # id of row to move
68 $to, # position to move it to (0 for first, 1 for 2nd, etc)
70 db_update('test_db_reposition', 'ord', $new_ord, 'where id=%i', $ids[$from]);
72 # move it in our cache
73 $moving = array_splice($ids, $from, 1);
75 $ids, # array to operate on
76 ($to > $from) ? ($to - 1) : ($to), # adjust if moving into the part we moved in the first splice
77 0, # don't delete anything
78 $moving # insert these
82 if (($tests % 10) === 9) {
83 $db_ids = db_get_column('test_db_reposition', 'id', 'order by ord');
84 for ($i = 0; $i < 10; ++$i) {
85 if ($ids[$i] != $db_ids[$i]) {
92 message("pass! $max_tests repositions checked every 10");