File: update.c

package info (click to toggle)
tarantool 1.5.2.20.g5f5d924-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,568 kB
  • ctags: 18,697
  • sloc: ansic: 109,092; sh: 21,312; cpp: 20,633; xml: 9,666; asm: 2,488; python: 2,195; java: 1,759; perl: 1,002; makefile: 679
file content (992 lines) | stat: -rw-r--r-- 26,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992

/*
 * Copyright (C) 2011 Mail.RU
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>

#include <connector/c/include/tarantool/tnt.h>
#include <connector/c/include/tarantool/tnt_net.h>
#include <connector/c/include/tarantool/tnt_io.h>

#include "tarantool/util.h"
#include <errcode.h>
#include <unit.h>

/*==========================================================================
 * test variables
 *==========================================================================*/

/** tarantool connector instance */
static struct tnt_stream *tnt;

static char *long_string = "A long time ago, in a galaxy far, far away...\n"
			   "It is a period of civil war. Rebel\n"
			   "spaceships, striking from a hidden\n"
			   "base, have won their first victory\n"
			   "against the evil Galactic Empire.\n"
			   "During the battle, Rebel spies managed\n"
			   "to steal secret plans to the Empire's\n"
			   "ultimate weapon, the Death Star, an\n"
			   "armored space station with enough\n"
			   "power to destroy an entire planet.\n"
			   "Pursued by the Empire's sinister agents,\n"
			   "Princess Leia races home aboard her\n"
			   "starship, custodian of the stolen plans\n"
			   "that can save her people and restore\n"
			   "freedom to the galaxy....";

/*==========================================================================
 * function declaration
 *==========================================================================*/

/*--------------------------------------------------------------------------
 * tarantool management functions
 *--------------------------------------------------------------------------*/

/** insert tuple */
void
insert_tuple(struct tnt_tuple *tuple);

/** select tuple by key */
void
select_tuple(int32_t key);

/** update fields */
void
update(int32_t key, struct tnt_stream *stream);

/** add update fields operation: set int32 */
void
update_set_i32(struct tnt_stream *stream, int32_t field, int32_t value);

/** add update fields operation: set string */
void
update_set_str(struct tnt_stream *stream, int32_t field, char *str);

/** add update fields operation: splice string */
void
update_splice_str(struct tnt_stream *stream, int32_t field, int32_t offset, int32_t length,
		  char *list);

/** add update fields operation: delete field */
void
update_delete_field(struct tnt_stream *stream, int32_t field);

/** add update fields operation: insert before int32 */
void
update_insert_i32(struct tnt_stream *stream, int32_t field, int32_t value);

/** add update fields operation: insert before string */
void
update_insert_str(struct tnt_stream *stream, int32_t field, char *str);

/** receive reply from server */
void
recv_command(char *command);

/** print tuple */
void
print_tuple(struct tnt_tuple *tuple);

/*--------------------------------------------------------------------------
 * test suite functions
 *--------------------------------------------------------------------------*/

/** setup test suite */
void
test_suite_setup();

/** clean-up test suite */
void
test_suite_tear_down();

/** print tarantool error message and exit */
void
fail_tnt_error(char *msg, int error_code);

/** print tarantool error message and exit */
void
fail_tnt_perror(char *msg);


/*--------------------------------------------------------------------------
 * test cases functions
 *--------------------------------------------------------------------------*/

/** update fields test case: simple set operation test */
void
test_simple_set();

/** update fields test case: long set operation test */
void
test_long_set();

/** update fields test case: append(set) operation test */
void
test_append();

/** update fields test case: 32-bit arithmetics operations test */
void
test_arith_i32();

/** update fields test case: 64-bit arithmetics operations test */
void
test_arith_i64();

/** update fields test case: multi arithmetics operations test */
void
test_multi_arith();

/** update fields test case: splice operations test */
void
test_splice();

/** update fields test case: set and spice operations test */
void
test_set_and_splice();

/** update fields test case: delete field operations test */
void
test_delete_field();

/** update fields test case: insert field operations test */
void
test_insert_field();

/** update fields test case: boundary arguments values test */
void
test_boundary_args();


/*==========================================================================
 * function definition
 *==========================================================================*/

int
main()
{
	/* initialize suite */
	test_suite_setup();
	/* run tests */
	test_simple_set();
	test_long_set();
	test_append();
	test_arith_i32();
	test_arith_i64();
	test_multi_arith();
	test_splice();
	test_set_and_splice();
	test_delete_field();
	test_insert_field();
	test_boundary_args();
	/* clean-up suite */
	test_suite_tear_down();
	return EXIT_SUCCESS;
}


/*--------------------------------------------------------------------------
 * tarantool management functions
 *--------------------------------------------------------------------------*/

void
insert_tuple(struct tnt_tuple *tuple)
{
	if (tnt_insert(tnt, 0, TNT_FLAG_RETURN, tuple) < 0)
		fail_tnt_perror("tnt_insert");
	if (tnt_flush(tnt) < 0)
		fail_tnt_perror("tnt_flush");
	recv_command("insert");
}

void
select_tuple(int32_t key)
{
	struct tnt_list tuple_list;
	tnt_list_init(&tuple_list);
	struct tnt_tuple *tuple = tnt_list_at(&tuple_list, NULL);
	tnt_tuple(tuple, "%d", key);
	if (tnt_select(tnt, 0, 0, 0, 1, &tuple_list) < 0)
		fail_tnt_perror("tnt_select");
	if (tnt_flush(tnt) < 0)
		fail_tnt_perror("tnt_flush");
	recv_command("select");
	tnt_list_free(&tuple_list);
}

void
update(int32_t key, struct tnt_stream *stream)
{
	struct tnt_tuple *k = tnt_tuple(NULL, "%d", key);
	if (tnt_update(tnt, 0, TNT_FLAG_RETURN, k, stream) < 0)
		fail_tnt_perror("tnt_update");
	if (tnt_flush(tnt) < 0)
		fail_tnt_perror("tnt_flush");
	tnt_tuple_free(k);
	recv_command("update fields");
}

void
update_set_i32(struct tnt_stream *stream, int32_t field, int32_t value)
{
	int result = tnt_update_assign(stream, field, (char *)&value, sizeof(value));
	if (result < 0)
		fail_tnt_error("tnt_update_assign", result);
}

void
update_set_str(struct tnt_stream *stream, int32_t field, char *str)
{
	int result = tnt_update_assign(stream, field, str, strlen(str));
	if (result < 0)
		fail_tnt_error("tnt_update_assign", result);
}

void
update_splice_str(struct tnt_stream *stream, int32_t field, int32_t offset, int32_t length,
		  char *list)
{
	int result = tnt_update_splice(stream, field, offset, length, list,
				       strlen(list));
	if (result < 0)
		fail_tnt_error("tnt_update_splice", result);
}

void
update_delete_field(struct tnt_stream *stream, int32_t field)
{
	int result = tnt_update_delete(stream, field);
	if (result < 0)
		fail_tnt_error("tnt_update_delete", result);
}

void
update_insert_i32(struct tnt_stream *stream, int32_t field, int32_t value)
{
	int result = tnt_update_insert(stream, field, (char *)&value,
				       sizeof(value));
	if (result < 0)
		fail_tnt_error("tnt_update_insert", result);
}

void
update_insert_str(struct tnt_stream *stream, int32_t field, char *str)
{
	int result = tnt_update_insert(stream, field, str, strlen(str));
	if (result < 0)
		fail_tnt_error("tnt_update_insert_before", result);
}

void
recv_command(char *command)
{
	struct tnt_iter i;
	tnt_iter_reply(&i, tnt);
	while (tnt_next(&i)) {
		struct tnt_reply *r = TNT_IREPLY_PTR(&i);
		printf("%s: respond %s (op: %"PRIu32", reqid: %"PRIu32", code: %"PRIu32", count: %"PRIu32")\n",
			command, tnt_strerror(tnt),
			r->op,
			r->reqid,
			r->code,
			r->count);
		struct tnt_iter it;
		tnt_iter_list(&it, TNT_REPLY_LIST(r));
		while (tnt_next(&it)) {
			struct tnt_tuple *tu = TNT_ILIST_TUPLE(&it);
			print_tuple(tu);
		}
		tnt_iter_free(&it);
	}
	if (i.status == TNT_ITER_FAIL)
		fail_tnt_perror("tnt_next");
	tnt_iter_free(&i);
}

void
print_tuple(struct tnt_tuple *tuple)
{
	bool is_first = true;
	printf("(");

	struct tnt_iter ifl;
	tnt_iter(&ifl, tuple);
	while (tnt_next(&ifl)) {
		char *data = TNT_IFIELD_DATA(&ifl);
		uint32_t size = TNT_IFIELD_SIZE(&ifl);
		if (!is_first) {
			printf(", ");
		}
		is_first = false;

		switch(size) {
		case 1:
			printf("%"PRIi8" (0x%02"PRIx8")", *(int8_t *)data, *(int8_t *)data);
			break;
		case 2:
			printf("%"PRIi16" (0x%04"PRIx16")", *(int16_t *)data, *(int16_t *)data);
			break;
		case 4:
			printf("%"PRIi32" (0x%08"PRIx32")", *(int32_t *)data, *(int32_t *)data);
			break;
		case 8:
			printf("%"PRIi64" (0x%016"PRIx64")", *(int64_t *)data, *(int64_t *)data);
			break;
		default:
			printf("'%.*s'", size, data);
			break;
		}
	}
	fail_if(ifl.status == TNT_ITER_FAIL);
	tnt_iter_free(&ifl);
	printf(")\n");
}


/*--------------------------------------------------------------------------
 * test suite functions
 *--------------------------------------------------------------------------*/

void
test_suite_setup()
{
	tnt = tnt_net(NULL);
	fail_if(tnt == NULL);

	tnt_set(tnt, TNT_OPT_HOSTNAME, "localhost");
	tnt_set(tnt, TNT_OPT_PORT, 33013);
	tnt_set(tnt, TNT_OPT_SEND_BUF, 128000);

	if (tnt_init(tnt) == -1)
		fail_tnt_perror("tnt_init");
	if (tnt_connect(tnt) == -1)
		fail_tnt_perror("tnt_connect");
}

void
test_suite_tear_down()
{
	tnt_stream_free(tnt);
}

void
fail_tnt_error(char *msg, int error_code)
{
	printf("fail: %s: %i\n", msg, error_code);
	exit(EXIT_FAILURE);
}

void
fail_tnt_perror(char *msg)
{
	printf("fail: %s: %s\n", msg, tnt_strerror(tnt));
	exit(EXIT_FAILURE);
}


/*--------------------------------------------------------------------------
 * test cases functions
 *--------------------------------------------------------------------------*/

void
test_simple_set()
{
	header();

	/* insert tuple */
	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%d%d%s", 1, 2, 0, "");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test simple set field\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update_set_str(stream, 1, "new field value");
	update_set_str(stream, 2, "");
	update_set_str(stream, 3, "fLaC");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# set field\n");
	stream = tnt_buf(NULL);
	update_set_str(stream, 1, "value?");
	update_set_str(stream, 1, "very very very very very long field value?");
	update_set_str(stream, 1, "field's new value");
	update(1, stream);
	tnt_stream_free(stream);

	stream = tnt_buf(NULL);
	printf("# test set primary key\n");
	update_set_i32(stream, 0, 2);
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_long_set()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s%s%s",
					    1, "first", "", "third");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test set big value in empty field\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update_set_str(stream, 2, long_string);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test replace long value to short\n");
	stream = tnt_buf(NULL);
	update_set_str(stream, 2, "short string");
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_append()
{
	header();

	/* insert tuple */
	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s", 1, "first");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	/* test append field */
	struct tnt_stream *stream = tnt_buf(NULL);
	printf("# test append field\n");
	update_set_str(stream, 2, "second");
	update(1, stream);
	tnt_stream_free(stream);

	/* test multi append field */
	stream = tnt_buf(NULL);
	printf("# test multi append\n");
	update_set_str(stream, 3, "3");
	update_set_str(stream, 3, "new field value");
	update_set_str(stream, 3, "other new field value");
	update_set_str(stream, 3, "third");
	update(1, stream);
	tnt_stream_free(stream);

	/* test append many field */
	stream = tnt_buf(NULL);
	printf("# test append many fields\n");
	update_set_str(stream, 4, "fourth");
	update_set_str(stream, 5, "fifth");
	update_set_str(stream, 6, "sixth");
	update_set_str(stream, 7, "seventh");
	update_set_str(stream, 8, long_string);
	update(1, stream);
	tnt_stream_free(stream);

	/* test append and change field */
	stream = tnt_buf(NULL);
	printf("# test append and change field\n");
	update_set_str(stream, 9, long_string);
	update_splice_str(stream, 9, 1, 544, "ac");
	tnt_update_arith_i32(stream, 9, TNT_UPDATE_XOR, 0x3ffffff);
	tnt_update_arith_i32(stream, 9, TNT_UPDATE_ADD, 1024);
	update(1, stream);
	tnt_stream_free(stream);

	/* test set to not an exist field */
	stream = tnt_buf(NULL);
	printf("# test set to not an exist field\n");
	update_set_str(stream, 0xDEADBEEF, "invalid!");
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_arith_i32()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%d%d%d", 1, 2, 0, 0);
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test add\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 16);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test overflow add\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, INT32_MAX);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test underflow add\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, INT32_MIN);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test or\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 2, TNT_UPDATE_OR, 0xbacfbacf);
	tnt_update_arith_i32(stream, 3, TNT_UPDATE_OR, 0xfabcfabc);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test xor\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 2, TNT_UPDATE_XOR, 0xffffffff);
	tnt_update_arith_i32(stream, 3, TNT_UPDATE_XOR, 0xffffffff);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test and\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 2, TNT_UPDATE_AND, 0xf0f0f0f0);
	tnt_update_arith_i32(stream, 3, TNT_UPDATE_AND, 0x0f0f0f0f);
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

/** update fields test case: 64-bit arithmetics operations test */
void
test_arith_i64()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%ll%ll%ll", 1, 2, 0, 0, 0);
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test add\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	tnt_update_arith_i64(stream, 1, TNT_UPDATE_ADD, 16);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test overflow add\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i64(stream, 1, TNT_UPDATE_ADD, INT64_MAX);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test underflow add\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i64(stream, 1, TNT_UPDATE_ADD, INT64_MIN);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test or\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i64(stream, 2, TNT_UPDATE_OR, 0xbacfbacfbacfbacf);
	tnt_update_arith_i64(stream, 3, TNT_UPDATE_OR, 0xfabcfabcfabcfabc);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test xor\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i64(stream, 2, TNT_UPDATE_XOR, 0xffffffffffffffff);
	tnt_update_arith_i64(stream, 3, TNT_UPDATE_XOR, 0xffffffffffffffff);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test and\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i64(stream, 2, TNT_UPDATE_AND, 0xf0f0f0f0f0f0f0f0);
	tnt_update_arith_i64(stream, 3, TNT_UPDATE_AND, 0x0f0f0f0f0f0f0f0f);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test casting 32-bit operand to 64-bit\n");
	stream = tnt_buf(NULL);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 16);
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_multi_arith()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s%d%s", 1, "first", 128, "third");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test simple and\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update_set_i32(stream, 2, 0);
	update_set_str(stream, 1, "first field new value");
	tnt_update_arith_i32(stream, 2, TNT_UPDATE_XOR, 0xF00F);
	update_set_str(stream, 3, "third field new value");
	tnt_update_arith_i32(stream, 2, TNT_UPDATE_OR, 0xF00F);
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_splice()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s%s%s", 1, "first", "hi, this is a test string!", "third");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	struct tnt_stream *stream = tnt_buf(NULL);
	printf("# test cut from begin\n");
	update_splice_str(stream, 2, 0, 4, "");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test cut from middle\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, 9, -8, "");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test cut from end\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, -1, 1, "");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test insert before begin\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, 0, 0, "Bonjour, ");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test insert after end\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, 10000, 0, " o_O!?");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test replace in begin\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, 0, 7, "Hello");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test replace in middle\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, 17, -6, "field");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test replace in end\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 2, -6, 4, "! Is this Sparta");
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_set_and_splice()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s%s%s", 1,
					    "first",
					    "hi, this is a test string!",
					    "third");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test set long string and splice to short\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update_set_str(stream, 2, long_string);
	update_splice_str(stream, 2, 45, 500, " away away away");
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test set short value and splice to long\n");
	stream = tnt_buf(NULL);
	update_set_str(stream, 2, "test");
	update_splice_str(stream, 2, -4, 4, long_string);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test splice to long and set to short\n");
	stream = tnt_buf(NULL);
	update_splice_str(stream, 3, -5, 5, long_string);
	update_set_str(stream, 2, "short name");	
	update(1, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_delete_field()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s%s%s%d%d%d%d%d%d%d%d%d%d",
					    1,
			                    "first",
					    "hi, this is a test string!",
					    "third",
					    1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test simple delete fields\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update_delete_field(stream, 2);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test useless operations with delete fields\n");
	stream = tnt_buf(NULL);
	update_set_i32(stream, 1, 0);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	update_delete_field(stream, 1);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test multi delete fields\n");
	stream = tnt_buf(NULL);
	update_delete_field(stream, 2);
	update_delete_field(stream, 3);
	update_delete_field(stream, 4);
	update_delete_field(stream, 5);
	update_delete_field(stream, 6);
	update_delete_field(stream, 7);
	update_delete_field(stream, 8);
	update_delete_field(stream, 9);
	update_delete_field(stream, 10);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test multi delete fields\n");
	stream = tnt_buf(NULL);
	update_delete_field(stream, 1);
	update_set_i32(stream, 1, 3);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test append and delete\n");
	stream = tnt_buf(NULL);
	update_set_str(stream, 3, "second");
	update_delete_field(stream, 3);
	update_set_str(stream, 3, "third");
	update_set_str(stream, 4, "third");
	update_delete_field(stream, 4);
	update_set_str(stream, 4, "third");
	update_set_str(stream, 4, "fourth");
	update_set_str(stream, 5, "fifth");
	update_set_str(stream, 6, "sixth");
	update_set_str(stream, 7, "seventh");
	update_set_str(stream, 8, "eighth");
	update_set_str(stream, 9, "ninth");
	update_delete_field(stream, 7);
	update_delete_field(stream, 6);
	update(1, stream);
	tnt_stream_free(stream);

	printf("# test double delete\n");
	stream = tnt_buf(NULL);
	update_delete_field(stream, 3);
	update_delete_field(stream, 3);
	update(1, stream);
	tnt_stream_free(stream);
	select_tuple(1);

	printf("# test delete not an exist field\n");
	stream = tnt_buf(NULL);
	update_delete_field(stream, 0xDEADBEEF);
	update(1, stream);
	tnt_stream_free(stream);
	select_tuple(1);

	footer();
}

void
test_insert_field()
{
	header();

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%s", 9, "eleven");
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# insert new field before primary key\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update_insert_i32(stream, 0, 7);
	update_insert_i32(stream, 0, 8);
	update(9, stream);
	tnt_stream_free(stream);

	printf("# insert a new field before last field\n");
	stream = tnt_buf(NULL);
	update_insert_i32(stream, 3, 10);
	update(7, stream);
	tnt_stream_free(stream);

	printf("# double insert at the end\n");
	stream = tnt_buf(NULL);
	update_set_i32(stream, 5, 14);
	update_insert_i32(stream, 6, 12);
	update_insert_i32(stream, 5, 13);
	update(7, stream);
	tnt_stream_free(stream);

	printf("# multi insert \n");
	stream = tnt_buf(NULL);
	update_insert_i32(stream, 5, 15);
	update_insert_i32(stream, 5, 14);
	update_insert_i32(stream, 5, 13);
	update_insert_i32(stream, 5, 12);
	update(7, stream);
	tnt_stream_free(stream);

	printf("# insert before next to last field\n");
	stream = tnt_buf(NULL);
	update_insert_i32(stream, 8, 15);
	update(7, stream);
	tnt_stream_free(stream);

	printf("# insert before next to last field\n");
	stream = tnt_buf(NULL);
	update_set_i32(stream, 9, 17);
	update_insert_i32(stream, 9, 16);
	update_set_i32(stream, 10, 19);
	update_insert_i32(stream, 10, 18);
	update(7, stream);
	tnt_stream_free(stream);

	printf("# insert second tuple\n");
	tuple = tnt_tuple(NULL, "%d%s%d", 0, "one", 11);
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	stream = tnt_buf(NULL);
	printf("# multi insert\n");
	update_set_i32(stream, 1, -11);
	tnt_update_arith(stream, 1, TNT_UPDATE_ADD, 1);
	update_insert_i32(stream, 1, 1);
	tnt_update_arith(stream, 1, TNT_UPDATE_ADD, 2);
	update_insert_i32(stream, 1, 2);
	update_insert_i32(stream, 1, 3);
	tnt_update_arith(stream, 1, TNT_UPDATE_ADD, 3);
	tnt_update_arith(stream, 1, TNT_UPDATE_ADD, 4);
	tnt_update_arith(stream, 1, TNT_UPDATE_ADD, 5);
	update_insert_i32(stream, 1, 4);
	update_insert_i32(stream, 1, 5);
	tnt_update_arith(stream, 1, TNT_UPDATE_ADD, 6);
	update_insert_i32(stream, 1, 6);
	update_insert_i32(stream, 1, 7);
	update_insert_i32(stream, 1, 8);
	update_insert_i32(stream, 1, 9);
	update(0, stream);
	tnt_stream_free(stream);

	printf("# insert before invalid field number\n");
	stream = tnt_buf(NULL);
	update_insert_str(stream, 100000, "ooppps!");
	update(7, stream);
	tnt_stream_free(stream);

	footer();
}

void
test_boundary_args()
{
	header();

	const int max_update_op_cnt = 4000;

	printf("# insert tuple\n");
	struct tnt_tuple *tuple = tnt_tuple(NULL, "%d%d", 0, 1);
	insert_tuple(tuple);
	tnt_tuple_free(tuple);

	printf("# test: try to do update w/o operations\n");
	struct tnt_stream *stream = tnt_buf(NULL);
	update(0, stream);
	tnt_stream_free(stream);

	printf("# test: update w/ maximal allowed opearions count\n");
	stream = tnt_buf(NULL);
	for (int i = 0; i < max_update_op_cnt; ++i)
		tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	update(0, stream);
	tnt_stream_free(stream);

	printf("# test: update w/ grater than maximal allowed opearions count\n");
	stream = tnt_buf(NULL);
	for (int i = 0; i < max_update_op_cnt + 1; ++i)
		tnt_update_arith_i32(stream, 1, TNT_UPDATE_ADD, 1);
	update(0, stream);
	tnt_stream_free(stream);

	footer();
}