#!/bin/sh

# osh
# Copyright (C) 2005 Jack Orenstein <jao@geophile.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

echo 'gen and out'
./smoketest_cli "osh gen 3 $" "[(0,), (1,), (2,)]"

echo 'f'
./smoketest_cli "osh gen 3 ^ f 'x: x * 10' $" "[(0,), (10,), (20,)]"

echo 'select'
./smoketest_cli "osh gen 4 ^ select 'x: x % 2 == 1' $" "[(1,), (3,)]"

echo 'agg'
./smoketest_cli "osh gen 5 ^ agg 0 'sum, x: sum + x' $" "[(10,)]"
./smoketest_cli "osh gen 5 ^ f 'x: (x / 2, x)' ^ agg -g 'halfx, x: halfx' 0 'sum, halfx, x: sum + x' $" "[(0, 1), (1, 5), (2, 4)]"
./smoketest_cli "osh gen 5 ^ f 'x: (x / 2, x)' ^ agg -c 'halfx, x: halfx' 0 'sum, halfx, x: sum + x' $" "[(0, 1), (1, 5), (2, 4)]"
./smoketest_cli "osh gen 5 ^ agg -r 0 'sum, x: sum + x' $" "[(0, 0), (1, 1), (3, 2), (6, 3), (10, 4)]"
./smoketest_cli "osh gen 5 ^ agg -r 0 'sum, x: sum + x' $" "[(0, 0), (1, 1), (3, 2), (6, 3), (10, 4)]"
./smoketest_cli "osh gen 5 ^ f 'x: (x / 2, x)' ^ agg -r -g 'halfx, x: halfx' 0 'sum, halfx, x: sum + x' $" "[(0, 0, 0), (1, 0, 1), (2, 1, 2), (5, 1, 3), (4, 2, 4)]"
./smoketest_cli "osh gen 5 ^ f 'x: (x / 2, x)' ^ agg -r -c 'halfx, x: halfx' 0 'sum, halfx, x: sum + x' $" "[(0, 0, 0), (1, 0, 1), (2, 1, 2), (5, 1, 3), (4, 2, 4)]"

echo 'sort'
./smoketest_cli "osh gen 5 ^ sort 'x: -x' $" "[(4,), (3,), (2,), (1,), (0,)]"

echo 'expand'
./smoketest_cli "osh gen 4 ^ f 'x: [x] * x' ^ expand $" "[(1,), (2,), (2,), (3,), (3,), (3,)]"
./smoketest_cli "osh gen 3 ^ f 'x: (x, (5, 6))' ^ expand 1 $" "[(0, 5), (0, 6), (1, 5), (1, 6), (2, 5), (2, 6)]"
./smoketest_cli "osh gen 3 ^ f 'x: (x, (5, 6))' ^ expand -1 $" "[(0, 5), (0, 6), (1, 5), (1, 6), (2, 5), (2, 6)]"
 
echo 'squish'
./smoketest_cli "osh gen 9 ^ window -d 3 ^ squish ^ squish + $" "[(3,), (12,), (21,)]"
./smoketest_cli "osh gen 27 ^ window -d 3 ^ squish + ^ window -d 3 ^ squish + max min $" "[(9, 7, 2), (36, 16, 11), (63, 25, 20)]"

echo 'window'
./smoketest_cli "osh gen 9 ^ window -d 3 ^ squish $" "[(0, 1, 2), (3, 4, 5), (6, 7, 8)]"
./smoketest_cli "osh gen 9 ^ window -o 3 ^ squish $" "[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, None), (8, None, None)]"
./smoketest_cli "osh gen 9 ^ window 'x: x % 3 == 0' ^ squish $" "[(0, 1, 2), (3, 4, 5), (6, 7, 8)]"

echo 'unique'
./smoketest_cli "osh gen 3 1 ^ f 'x: [x for i in range(x)]' ^ expand ^ unique ^ sort $" "[(1,), (2,), (3,)]"
./smoketest_cli "osh gen 3 1 ^ f 'x: [x for i in range(x)]' ^ expand ^ sort ^ unique -c $" "[(1,), (2,), (3,)]"

echo 'stdin and sh'
./smoketest_cli "echo 'abc' | osh ^ f 's: [c for c in s]' $" "[('a', 'b', 'c')]"
./smoketest_cli "osh sh 'echo abc' ^ f 's: [c for c in s]' $" "[('a', 'b', 'c')]"

echo 'builtins'
./smoketest_cli "osh gen 3 ^ f 'x: ifelse(x == 0, 999, x)' $" "[(999,), (1,), (2,)]"
 
echo 'pipeline'
./smoketest_cli "osh [ gen 3 ] ^ f 'x: 10 * x' $" "[(0,), (10,), (20,)]"
./smoketest_cli "osh gen 3 ^ [ f 'x: 10 * x' ] $" "[(0,), (10,), (20,)]"
./smoketest_cli "osh [ gen 3  ^ f 'x: 10 * x' ] $" "[(0,), (10,), (20,)]"
./smoketest_cli "osh [ gen 3  ^ [ f 'x: 10 * x' ] ] $" "[(0,), (10,), (20,)]"
