var test = require('tape')

function all (db, range, cb) {
  var o = {}
  db.createReadStream(range)
    .on('data', function (data) {
      o[data.key] = data.value
    })
    .on('end', function () {
      cb(null, o)
    })
}

function makeTest(db, name) {

  test(name, function (t) {

    t.plan(10)

    var docs = {
      a: 'apple',
      b: 'banana',
      c: 'cherry',
      d: 'durian',
      e: 'elder-berry'
    }

    db.batch(Object.keys(docs).map(function (key) {
      console.log(key, docs[key])
      return {key: key, value: docs[key], type: 'put'}
    }), function (err) {
      t.notOk(err) 

      all(db, {}, function (err, all) {
        t.deepEqual(all, docs)
      })

      all(db, {min: 'a~'}, function (err, all) {
        t.deepEqual(all, {
          b: 'banana',
          c: 'cherry',
          d: 'durian',
          e: 'elder-berry'
        })
      })

      all(db, {min: 'b'}, function (err, all) {
        t.deepEqual(all, {
          b: 'banana',
          c: 'cherry',
          d: 'durian',
          e: 'elder-berry'
        })
      })


      all(db, {min: 'a~', reverse: true}, function (err, all) {
        t.deepEqual(all, {
          b: 'banana',
          c: 'cherry',
          d: 'durian',
          e: 'elder-berry'
        })
      })

      all(db, {min: 'c~', reverse: true}, function (err, all) {
        console.log(all)
        t.deepEqual(all, {
          d: 'durian',
          e: 'elder-berry'
        })
      })

      all(db, {min: 'c~', max: 'd~'}, function (err, all) {
        console.log(all)
        t.deepEqual(all, {
          d: 'durian',
        })
      })

      all(db, {min: 'a~'}, function (err, all) {
        t.deepEqual(all, {
          b: 'banana',
          c: 'cherry',
          d: 'durian',
          e: 'elder-berry'
        })
      })

      all(db, {min: 'c~'}, function (err, all) {
        console.log(all)
        t.deepEqual(all, {
          d: 'durian',
          e: 'elder-berry'
        })
      })

      all(db, {min: 'c~', max: 'd~', reverse: true}, function (err, all) {
        console.log(all)
        t.deepEqual(all, {
          d: 'durian',
        })
      })
    })
  })
}



require('rimraf').sync('/tmp/test-sublevel-reverse')

var base = require('../')
  (require('levelup')('/tmp/test-sublevel-reverse'))
var A = base.sublevel('A')
makeTest(base, 'simple')

makeTest(A, 'sublevel')

makeTest(base, 'simple, again')

var A_B = A.sublevel('B')
makeTest(A_B, 'sublevel2')

makeTest(A, 'sublevel, again')

makeTest(base, 'simple, again 2')


@@ -299,9 +299,9 @@
 an(1
-0
+9
 )%0A%0A 
@@ -421,16 +421,126 @@
 %0A    %7D%0A%0A
+    function order(a, b) %7B%0A      t.deepEqual(a, b)%0A      t.equal(JSON.stringify(a), JSON.stringify(b))%0A    %7D%0A%0A
     db.b
@@ -762,35 +762,29 @@
 ) %7B%0A        
-t.deepEqual
+order
 (all, docs)%0A
@@ -846,35 +846,29 @@
 ) %7B%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -1032,35 +1032,29 @@
 ) %7B%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -1235,35 +1235,29 @@
 ) %7B%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -1258,34 +1258,39 @@
 %7B%0A          
-b: 'banana
+e: 'elder-berry
 ',%0A         
@@ -1286,34 +1286,34 @@
 ,%0A          
-c: 'cherry
+d: 'durian
 ',%0A         
@@ -1309,34 +1309,34 @@
 ,%0A          
-d: 'durian
+c: 'cherry
 ',%0A         
@@ -1332,39 +1332,34 @@
 ,%0A          
-e: 'elder-berry
+b: 'banana
 '%0A        %7D)
@@ -1462,35 +1462,29 @@
 ll)%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -1485,34 +1485,39 @@
 %7B%0A          
-d: 'durian
+e: 'elder-berry
 ',%0A         
@@ -1513,39 +1513,34 @@
 ,%0A          
-e: 'elder-berry
+d: 'durian
 '%0A        %7D)
@@ -1639,35 +1639,29 @@
 ll)%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -1753,35 +1753,29 @@
 ) %7B%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -1948,32 +1948,40 @@
     console.log(
+'d, e', 
 all)%0A        t.d
@@ -1973,35 +1973,29 @@
 ll)%0A        
-t.deepEqual
+order
 (all, %7B%0A    
@@ -2169,27 +2169,21 @@
         
-t.deepEqual
+order
 (all, %7B%0A

fix broken tests - deepEqual does not care about order!