<?php
namespace ccxt\pro;
include_once (__DIR__.'/../../../../ccxt.php');
// ----------------------------------------------------------------------------

// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

// -----------------------------------------------------------------------------


// --------------------------------------------------------------------------------------------------------------------
function test_ws_order_book() {
    $order_book_input = array(
        'bids' => [[10, 10], [9.1, 11], [8.2, 12], [7.3, 13], [6.4, 14], [4.5, 13], [4.5, 0]],
        'asks' => [[16.6, 10], [15.5, 11], [14.4, 12], [13.3, 13], [12.2, 14], [11.1, 13]],
        'timestamp' => 1574827239000,
        'nonce' => 69,
        'symbol' => null,
    );
    $order_book_target = array(
        'bids' => [[10, 10], [9.1, 11], [8.2, 12], [7.3, 13], [6.4, 14]],
        'asks' => [[11.1, 13], [12.2, 14], [13.3, 13], [14.4, 12], [15.5, 11], [16.6, 10]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $store_bid = array(
        'bids' => [[10, 10], [9.1, 11], [8.2, 12], [7.3, 13], [6.4, 14], [3, 4]],
        'asks' => [[11.1, 13], [12.2, 14], [13.3, 13], [14.4, 12], [15.5, 11], [16.6, 10]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $limited_order_book_target = array(
        'bids' => [[10, 10], [9.1, 11], [8.2, 12], [7.3, 13], [6.4, 14]],
        'asks' => [[11.1, 13], [12.2, 14], [13.3, 13], [14.4, 12], [15.5, 11]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $limited_deleted_order_book_target = array(
        'bids' => [[10, 10], [9.1, 11], [8.2, 12], [7.3, 13], [6.4, 14]],
        'asks' => [[11.1, 13], [12.2, 14], [13.3, 13], [14.4, 12]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $indexed_order_book_input = array(
        'bids' => [[10, 10, '1234'], [9.1, 11, '1235'], [8.2, 12, '1236'], [7.3, 13, '1237'], [6.4, 14, '1238'], [4.5, 13, '1239']],
        'asks' => [[16.6, 10, '1240'], [15.5, 11, '1241'], [14.4, 12, '1242'], [13.3, 13, '1243'], [12.2, 14, '1244'], [11.1, 13, '1244']],
        'timestamp' => 1574827239000,
        'nonce' => 69,
        'symbol' => null,
    );
    $indexed_order_book_target = array(
        'bids' => [[10, 10, '1234'], [9.1, 11, '1235'], [8.2, 12, '1236'], [7.3, 13, '1237'], [6.4, 14, '1238'], [4.5, 13, '1239']],
        'asks' => [[11.1, 13, '1244'], [13.3, 13, '1243'], [14.4, 12, '1242'], [15.5, 11, '1241'], [16.6, 10, '1240']],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $limited_indexed_order_book_target = array(
        'bids' => [[10, 10, '1234'], [9.1, 11, '1235'], [8.2, 12, '1236'], [7.3, 13, '1237'], [6.4, 14, '1238']],
        'asks' => [[11.1, 13, '1244'], [13.3, 13, '1243'], [14.4, 12, '1242'], [15.5, 11, '1241'], [16.6, 10, '1240']],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    // const incrementalIndexedOrderBookTarget = {
    //     'bids': [ [ 10.0, 10, '1234' ], [ 9.1, 11, '1235' ], [ 8.2, 12, '1236' ], [ 7.3, 13, '1237' ], [ 6.4, 14, '1238' ], [ 4.5, 13, '1239' ] ],
    //     'asks': [ [ 11.1, 27, '1244' ], [ 13.3, 13, '1243' ], [ 14.4, 12, '1242' ], [ 15.5, 11, '1241' ], [ 16.6, 10, '1240' ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const incrementalIndexedOrderBookDeletedTarget = {
    //     'bids': [ [ 9.1, 11, '1235' ], [ 8.2, 12, '1236' ], [ 7.3, 13, '1237' ], [ 6.4, 14, '1238' ], [ 4.5, 13, '1239' ] ],
    //     'asks': [ [ 11.1, 27, '1244' ], [ 13.3, 13, '1243' ], [ 14.4, 12, '1242' ], [ 15.5, 11, '1241' ], [ 16.6, 10, '1240' ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const limitedIncrementalIndexedOrderBookTarget = {
    //     'bids': [ [ 10.0, 10, '1234' ], [ 9.1, 11, '1235' ], [ 8.2, 12, '1236' ], [ 7.3, 13, '1237' ], [ 6.4, 14, '1238' ] ],
    //     'asks': [ [ 11.1, 27, '1244' ], [ 13.3, 13, '1243' ], [ 14.4, 12, '1242' ], [ 15.5, 11, '1241' ], [ 16.6, 10, '1240' ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const storedIncrementalIndexedOrderBookTarget = {
    //     'bids': [ [ 10.0, 13, '1234' ], [ 9.1, 11, '1235' ], [ 8.2, 12, '1236' ], [ 7.3, 13, '1237' ], [ 6.4, 14, '1238' ], [ 4.5, 13, '1239' ] ],
    //     'asks': [ [ 11.1, 27, '1244' ], [ 13.3, 13, '1243' ], [ 14.4, 12, '1242' ], [ 15.5, 11, '1241' ], [ 16.6, 10, '1240' ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const anotherStoredIncrementalIndexedOrderBookTarget = {
    //     'bids': [ [ 10.2, 13, '1234' ], [ 9.1, 11, '1235' ], [ 8.2, 12, '1236' ], [ 7.3, 13, '1237' ], [ 6.4, 14, '1238' ], [ 4.5, 13, '1239' ] ],
    //     'asks': [ [ 11.1, 27, '1244' ], [ 13.3, 13, '1243' ], [ 14.4, 12, '1242' ], [ 15.5, 11, '1241' ], [ 16.6, 10, '1240' ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    $overwrite1234 = array(
        'bids' => [[9.1, 11, '1235'], [9, 3, '1231'], [9, 1, '1232'], [8.2, 12, '1236'], [7.3, 13, '1237'], [6.4, 14, '1238'], [4.5, 13, '1239'], [4, 2, '12399']],
        'asks' => [[11.1, 13, '1244'], [13.3, 13, '1243'], [14.4, 12, '1242'], [15.5, 11, '1241'], [16.6, 10, '1240']],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $overwrite1244 = array(
        'bids' => [[10, 10, '1234'], [9.1, 11, '1235'], [8.2, 12, '1236'], [7.3, 13, '1237'], [6.4, 14, '1238'], [4.5, 13, '1239']],
        'asks' => [[13.3, 13, '1243'], [13.5, 13, '1244'], [14.4, 12, '1242'], [15.5, 11, '1241'], [16.6, 10, '1240']],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $counted_order_book_input = array(
        'bids' => [[10, 10, 1], [9.1, 11, 1], [8.2, 12, 1], [7.3, 13, 1], [7.3, 0, 1], [6.4, 14, 5], [4.5, 13, 5], [4.5, 13, 1], [4.5, 13, 0]],
        'asks' => [[16.6, 10, 1], [15.5, 11, 1], [14.4, 12, 1], [13.3, 13, 3], [12.2, 14, 3], [11.1, 13, 3], [11.1, 13, 12]],
        'timestamp' => 1574827239000,
        'nonce' => 69,
        'symbol' => null,
    );
    $counted_order_book_target = array(
        'bids' => [[10, 10, 1], [9.1, 11, 1], [8.2, 12, 1], [6.4, 14, 5]],
        'asks' => [[11.1, 13, 12], [12.2, 14, 3], [13.3, 13, 3], [14.4, 12, 1], [15.5, 11, 1], [16.6, 10, 1]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $stored_counted_orderbook_target = array(
        'bids' => [[10, 10, 1], [9.1, 11, 1], [8.2, 12, 1], [6.4, 14, 5], [1, 1, 6]],
        'asks' => [[11.1, 13, 12], [12.2, 14, 3], [13.3, 13, 3], [14.4, 12, 1], [15.5, 11, 1], [16.6, 10, 1]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    $limited_counted_order_book_target = array(
        'bids' => [[10, 10, 1], [9.1, 11, 1], [8.2, 12, 1], [6.4, 14, 5]],
        'asks' => [[11.1, 13, 12], [12.2, 14, 3], [13.3, 13, 3], [14.4, 12, 1], [15.5, 11, 1]],
        'timestamp' => 1574827239000,
        'datetime' => '2019-11-27T04:00:39.000Z',
        'nonce' => 69,
        'symbol' => null,
    );
    // const incrementalOrderBookInput = {
    //     'bids': [ [ 10.0, 1 ], [ 10.0, 2 ], [ 9.1, 0 ], [ 8.2, 1 ], [ 7.3, 1 ], [ 6.4, 1 ] ],
    //     'asks': [ [ 11.1, 5 ], [ 11.1, -6 ], [ 11.1, 2 ], [ 12.2, 10 ], [ 12.2, -9.875 ], [ 12.2, 0 ], [ 13.3, 3 ], [ 14.4, 4 ], [ 15.5, 1 ], [ 16.6, 3 ] ],
    //     'timestamp': 1574827239000,
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const incremetalOrderBookTarget = {
    //     'bids': [ [ 10.0, 3 ], [ 8.2, 1 ], [ 7.3, 1 ], [ 6.4, 1 ] ],
    //     'asks': [ [ 11.1, 2 ], [ 12.2, 0.125 ], [ 13.3, 3 ], [ 14.4, 4 ], [ 15.5, 1 ], [ 16.6, 3 ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const limitedIncremetalOrderBookTarget = {
    //     'bids': [ [ 10.0, 3 ], [ 8.2, 1 ], [ 7.3, 1 ], [ 6.4, 1 ] ],
    //     'asks': [ [ 11.1, 2 ], [ 12.2, 0.125 ], [ 13.3, 3 ], [ 14.4, 4 ], [ 15.5, 1 ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const storedIncremetalOrderBookTarget = {
    //     'bids': [ [ 10.0, 3 ], [ 8.2, 1 ], [ 7.3, 1 ], [ 6.4, 1 ], [ 3, 3 ] ],
    //     'asks': [ [ 11.1, 2 ], [ 12.2, 0.125 ], [ 13.3, 3 ], [ 14.4, 4 ], [ 15.5, 1 ], [ 16.6, 3 ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const doubleStoredIncremetalOrderBookTarget = {
    //     'bids': [ [ 10.0, 3 ], [ 8.2, 1 ], [ 7.3, 1 ], [ 6.4, 1 ], [ 3, 10 ] ],
    //     'asks': [ [ 11.1, 2 ], [ 12.2, 0.125 ], [ 13.3, 3 ], [ 14.4, 4 ], [ 15.5, 1 ], [ 16.6, 3 ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // const negativeStoredIncremetalOrderBookTarget = {
    //     'bids': [ [ 10.0, 3 ], [ 8.2, 1 ], [ 7.3, 1 ], [ 6.4, 1 ] ],
    //     'asks': [ [ 11.1, 2 ], [ 12.2, 0.125 ], [ 13.3, 3 ], [ 14.4, 4 ], [ 16.6, 3 ] ],
    //     'timestamp': 1574827239000,
    //     'datetime': '2019-11-27T04:00:39.000Z',
    //     'nonce': 69,
    //     'symbol': undefined,
    // };
    // --------------------------------------------------------------------------------------------------------------------
    $order_book = new OrderBook($order_book_input);
    $limited = new OrderBook($order_book_input, 5);
    $order_book->limit();
    assert(equals($order_book, $order_book_target));
    $limited->limit();
    assert(equals($limited, $limited_order_book_target));
    $order_book->limit();
    assert(equals($order_book, $order_book_target));
    $bids = $order_book['bids'];
    $bids->store(1000, 0);
    $order_book->limit();
    assert(equals($order_book, $order_book_target));
    $bids->store(3, 4);
    $order_book->limit();
    assert(equals($order_book, $store_bid));
    $bids->store(3, 0);
    $order_book->limit();
    assert(equals($order_book, $order_book_target));
    $asks = $limited['asks'];
    $asks->store(15.5, 0);
    $limited->limit();
    assert(equals($limited, $limited_deleted_order_book_target));
    // --------------------------------------------------------------------------------------------------------------------
    $indexed_order_book = new IndexedOrderBook($indexed_order_book_input);
    $limited_indexed_order_book = new IndexedOrderBook($indexed_order_book_input, 5);
    $indexed_order_book->limit();
    assert(equals($indexed_order_book, $indexed_order_book_target));
    $limited_indexed_order_book->limit();
    assert(equals($limited_indexed_order_book, $limited_indexed_order_book_target));
    $indexed_order_book->limit();
    assert(equals($indexed_order_book, $indexed_order_book_target));
    $indexed_bids = $indexed_order_book['bids'];
    $indexed_bids->store_array([1000, 0, '12345']);
    assert(equals($indexed_order_book, $indexed_order_book_target));
    $indexed_bids->store_array([10, 0, '1234']);
    $indexed_bids->store_array([10, 2, '1231']);
    $indexed_bids->store_array([10, 1, '1232']);
    $indexed_bids->store_array([4, 2, '12399']);
    $indexed_bids->store_array([9, 2, '1231']);
    $indexed_bids->store_array([9, 3, '1231']);
    $indexed_bids->store_array([9, 1, '1232']);
    $indexed_order_book->limit();
    assert(equals($indexed_order_book, $overwrite1234));
    $indexed_order_book = new IndexedOrderBook($indexed_order_book_input);
    $indexed_asks = $indexed_order_book['asks'];
    $indexed_asks->store_array([13.5, 13, '1244']);
    $indexed_order_book->limit();
    assert(equals($indexed_order_book, $overwrite1244));
    // --------------------------------------------------------------------------------------------------------------------
    $counted_order_book = new CountedOrderBook($counted_order_book_input);
    $limited_counted_order_book = new CountedOrderBook($counted_order_book_input, 5);
    $counted_order_book->limit();
    assert(equals($counted_order_book, $counted_order_book_target));
    $limited_counted_order_book->limit();
    assert(equals($limited_counted_order_book, $limited_counted_order_book_target));
    $counted_order_book->limit();
    assert(equals($counted_order_book, $counted_order_book_target));
    $counted_bids = $counted_order_book['bids'];
    $counted_bids->store_array([5, 0, 6]);
    $counted_order_book->limit();
    assert(equals($counted_order_book, $counted_order_book_target));
    $counted_bids->store_array([1, 1, 6]);
    $counted_order_book->limit();
    assert(equals($counted_order_book, $stored_counted_orderbook_target));
    // --------------------------------------------------------------------------------------------------------------------
    // let incrementalOrderBook = new IncrementalOrderBook (incrementalOrderBookInput);
    // const limitedIncrementalOrderBook = new IncrementalOrderBook (incrementalOrderBookInput, 5);
    // incrementalOrderBook.limit ();
    // assert (equals (incrementalOrderBook, incremetalOrderBookTarget));
    // incrementalOrderBook.limit (5);
    // limitedIncrementalOrderBook.limit ();
    // assert (equals (incrementalOrderBook, limitedIncremetalOrderBookTarget));
    // assert (equals (limitedIncrementalOrderBook, limitedIncremetalOrderBookTarget));
    // incrementalOrderBook.limit ();
    // assert (equals (incrementalOrderBook, incremetalOrderBookTarget));
    // bids = incrementalOrderBook['bids'];
    // bids.store (3, 3);
    // incrementalOrderBook.limit ();
    // assert (equals (incrementalOrderBook, storedIncremetalOrderBookTarget));
    // bids.store (3, 7);
    // incrementalOrderBook.limit ();
    // assert (equals (incrementalOrderBook, doubleStoredIncremetalOrderBookTarget));
    // bids.store (17, 0);
    // assert (equals (incrementalOrderBook, doubleStoredIncremetalOrderBookTarget));
    // incrementalOrderBook = new IncrementalOrderBook (incrementalOrderBookInput);
    // asks = incrementalOrderBook['asks'];
    // asks.store (15.5, -10);
    // incrementalOrderBook.limit ();
    // assert (equals (incrementalOrderBook, negativeStoredIncremetalOrderBookTarget));
    // --------------------------------------------------------------------------------------------------------------------
    // let incrementalIndexedOrderBook = new IncrementalIndexedOrderBook (indexedOrderBookInput);
    // const limitedIncrementalIndexedOrderBook = new IncrementalIndexedOrderBook (indexedOrderBookInput, 5);
    // incrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, incrementalIndexedOrderBookTarget));
    // incrementalIndexedOrderBook.limit (5);
    // limitedIncrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, limitedIncrementalIndexedOrderBookTarget));
    // assert (equals (limitedIncrementalIndexedOrderBook, limitedIncrementalIndexedOrderBookTarget));
    // incrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, incrementalIndexedOrderBookTarget));
    // bids = incrementalIndexedOrderBook['bids'];
    // bids.store (5, 0, 'xxyy');
    // incrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, incrementalIndexedOrderBookTarget));
    // bids.store (10.0, 3, '1234');  // price does match merge size
    // incrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, storedIncrementalIndexedOrderBookTarget));
    // bids.store (0, 0, '1234');
    // incrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, incrementalIndexedOrderBookDeletedTarget));
    // incrementalIndexedOrderBook = new IncrementalIndexedOrderBook (indexedOrderBookInput);
    // bids = incrementalIndexedOrderBook['bids'];
    // bids.store (10.2, 3, '1234');  // price does not match merge size
    // incrementalIndexedOrderBook.limit ();
    // assert (equals (incrementalIndexedOrderBook, anotherStoredIncrementalIndexedOrderBookTarget));
    // --------------------------------------------------------------------------------------------------------------------
    $reset_book = new OrderBook($store_bid);
    $reset_book->limit();
    $reset_book->reset($order_book_input);
    $reset_book->limit();
    assert(equals($reset_book, $order_book_target));
}
