Skip to content

Commit

Permalink
patch 8.2.2777: Vim9: blob operations not tested in all ways
Browse files Browse the repository at this point in the history
Problem:    Vim9: blob operations not tested in all ways.
Solution:   Run tests with CheckLegacyAndVim9Success().  Make blob assign with
            index work.
  • Loading branch information
brammool committed Apr 17, 2021
1 parent 0995c81 commit 51e9332
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 96 deletions.
26 changes: 23 additions & 3 deletions src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,33 @@ blob_get(blob_T *b, int idx)
}

/*
* Store one byte "c" in blob "b" at "idx".
* Store one byte "byte" in blob "blob" at "idx".
* Caller must make sure that "idx" is valid.
*/
void
blob_set(blob_T *b, int idx, char_u c)
blob_set(blob_T *blob, int idx, int byte)
{
((char_u*)b->bv_ga.ga_data)[idx] = c;
((char_u*)blob->bv_ga.ga_data)[idx] = byte;
}

/*
* Store one byte "byte" in blob "blob" at "idx".
* Append one byte if needed.
*/
void
blob_set_append(blob_T *blob, int idx, int byte)
{
garray_T *gap = &blob->bv_ga;

// Allow for appending a byte. Setting a byte beyond
// the end is an error otherwise.
if (idx < gap->ga_len
|| (idx == gap->ga_len && ga_grow(gap, 1) == OK))
{
blob_set(blob, idx, byte);
if (idx == gap->ga_len)
++gap->ga_len;
}
}

/*
Expand Down
2 changes: 2 additions & 0 deletions src/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,5 @@ EXTERN char e_blob_required[]
INIT(= N_("E1182: Blob required"));
EXTERN char e_cannot_use_range_with_assignment_operator_str[]
INIT(= N_("E1183: Cannot use a range with an assignment operator: %s"));
EXTERN char e_blob_not_set[]
INIT(= N_("E1184: Blob not set"));
3 changes: 2 additions & 1 deletion src/proto/blob.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ void blob_free(blob_T *b);
void blob_unref(blob_T *b);
long blob_len(blob_T *b);
int blob_get(blob_T *b, int idx);
void blob_set(blob_T *b, int idx, char_u c);
void blob_set(blob_T *blob, int idx, int byte);
void blob_set_append(blob_T *blob, int idx, int byte);
int blob_equal(blob_T *b1, blob_T *b2);
int read_blob(FILE *fd, blob_T *blob);
int write_blob(FILE *fd, blob_T *blob);
Expand Down
216 changes: 147 additions & 69 deletions src/testdir/test_blob.vim
Original file line number Diff line number Diff line change
Expand Up @@ -120,88 +120,166 @@ func Test_blob_assign()
endfunc

func Test_blob_get_range()
let lines =<< trim END
VAR b = 0z0011223344
call assert_equal(0z2233, b[2 : 3])
call assert_equal(0z223344, b[2 : -1])
call assert_equal(0z00, b[0 : -5])
call assert_equal(0z, b[0 : -11])
call assert_equal(0z44, b[-1 :])
call assert_equal(0z0011223344, b[:])
call assert_equal(0z0011223344, b[: -1])
call assert_equal(0z, b[5 : 6])
call assert_equal(0z0011, b[-10 : 1])
END
call CheckLegacyAndVim9Success(lines)

" legacy script white space
let b = 0z0011223344
call assert_equal(0z2233, b[2:3])
call assert_equal(0z223344, b[2:-1])
call assert_equal(0z00, b[0:-5])
call assert_equal(0z, b[0:-11])
call assert_equal(0z44, b[-1:])
call assert_equal(0z0011223344, b[:])
call assert_equal(0z0011223344, b[:-1])
call assert_equal(0z, b[5:6])
call assert_equal(0z0011, b[-10:1])
endfunc

func Test_blob_get()
let b = 0z0011223344
call assert_equal(0x00, get(b, 0))
call assert_equal(0x22, get(b, 2, 999))
call assert_equal(0x44, get(b, 4))
call assert_equal(0x44, get(b, -1))
call assert_equal(-1, get(b, 5))
call assert_equal(999, get(b, 5, 999))
call assert_equal(-1, get(b, -8))
call assert_equal(999, get(b, -8, 999))
call assert_equal(10, get(test_null_blob(), 2, 10))

call assert_equal(0x00, b[0])
call assert_equal(0x22, b[2])
call assert_equal(0x44, b[4])
call assert_equal(0x44, b[-1])
call assert_fails('echo b[5]', 'E979:')
call assert_fails('echo b[-8]', 'E979:')
let lines =<< trim END
VAR b = 0z0011223344
call assert_equal(0x00, get(b, 0))
call assert_equal(0x22, get(b, 2, 999))
call assert_equal(0x44, get(b, 4))
call assert_equal(0x44, get(b, -1))
call assert_equal(-1, get(b, 5))
call assert_equal(999, get(b, 5, 999))
call assert_equal(-1, get(b, -8))
call assert_equal(999, get(b, -8, 999))
call assert_equal(10, get(test_null_blob(), 2, 10))

call assert_equal(0x00, b[0])
call assert_equal(0x22, b[2])
call assert_equal(0x44, b[4])
call assert_equal(0x44, b[-1])
END
call CheckLegacyAndVim9Success(lines)

let lines =<< trim END
VAR b = 0z0011223344
echo b[5]
END
call CheckLegacyAndVim9Failure(lines, 'E979:')

let lines =<< trim END
VAR b = 0z0011223344
echo b[-8]
END
call CheckLegacyAndVim9Failure(lines, 'E979:')
endfunc

func Test_blob_to_string()
let b = 0z00112233445566778899aabbccdd
call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
call assert_equal(b, eval(string(b)))
call remove(b, 4, -1)
call assert_equal('0z00112233', string(b))
call remove(b, 0, 3)
call assert_equal('0z', string(b))
call assert_equal('0z', string(test_null_blob()))
let lines =<< trim END
VAR b = 0z00112233445566778899aabbccdd
call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
call assert_equal(b, eval(string(b)))
call remove(b, 4, -1)
call assert_equal('0z00112233', string(b))
call remove(b, 0, 3)
call assert_equal('0z', string(b))
call assert_equal('0z', string(test_null_blob()))
END
call CheckLegacyAndVim9Success(lines)
endfunc

func Test_blob_compare()
let b1 = 0z0011
let b2 = 0z1100
let b3 = 0z001122
call assert_true(b1 == b1)
call assert_false(b1 == b2)
call assert_false(b1 == b3)
call assert_true(b1 != b2)
call assert_true(b1 != b3)
call assert_true(b1 == 0z0011)
call assert_fails('echo b1 == 9', 'E977:')
call assert_fails('echo b1 != 9', 'E977:')

call assert_false(b1 is b2)
let b2 = b1
call assert_true(b1 == b2)
call assert_true(b1 is b2)
let b2 = copy(b1)
call assert_true(b1 == b2)
call assert_false(b1 is b2)
let b2 = b1[:]
call assert_true(b1 == b2)
call assert_false(b1 is b2)
call assert_true(b1 isnot b2)

call assert_fails('let x = b1 > b2')
call assert_fails('let x = b1 < b2')
call assert_fails('let x = b1 - b2')
call assert_fails('let x = b1 / b2')
call assert_fails('let x = b1 * b2')
let lines =<< trim END
VAR b1 = 0z0011
VAR b2 = 0z1100
VAR b3 = 0z001122
call assert_true(b1 == b1)
call assert_false(b1 == b2)
call assert_false(b1 == b3)
call assert_true(b1 != b2)
call assert_true(b1 != b3)
call assert_true(b1 == 0z0011)

call assert_false(b1 is b2)
LET b2 = b1
call assert_true(b1 == b2)
call assert_true(b1 is b2)
LET b2 = copy(b1)
call assert_true(b1 == b2)
call assert_false(b1 is b2)
LET b2 = b1[:]
call assert_true(b1 == b2)
call assert_false(b1 is b2)
call assert_true(b1 isnot b2)
END
call CheckLegacyAndVim9Success(lines)

let lines =<< trim END
VAR b1 = 0z0011
echo b1 == 9
END
call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])

let lines =<< trim END
VAR b1 = 0z0011
echo b1 != 9
END
call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])

let lines =<< trim END
VAR b1 = 0z0011
VAR b2 = 0z1100
VAR x = b1 > b2
END
call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])

let lines =<< trim END
VAR b1 = 0z0011
VAR b2 = 0z1100
VAR x = b1 < b2
END
call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])

let lines =<< trim END
VAR b1 = 0z0011
VAR b2 = 0z1100
VAR x = b1 - b2
END
call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])

let lines =<< trim END
VAR b1 = 0z0011
VAR b2 = 0z1100
VAR x = b1 / b2
END
call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])

let lines =<< trim END
VAR b1 = 0z0011
VAR b2 = 0z1100
VAR x = b1 * b2
END
call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
endfunc

" test for range assign
func Test_blob_range_assign()
let b = 0z00
let b[1] = 0x11
let b[2] = 0x22
call assert_equal(0z001122, b)
call assert_fails('let b[4] = 0x33', 'E979:')
func Test_blob_index_assign()
let lines =<< trim END
VAR b = 0z00
LET b[1] = 0x11
LET b[2] = 0x22
call assert_equal(0z001122, b)
END
call CheckLegacyAndVim9Success(lines)

let lines =<< trim END
VAR b = 0z00
LET b[2] = 0x33
END
call CheckLegacyAndVim9Failure(lines, 'E979:')

let lines =<< trim END
VAR b = 0z00
LET b[-2] = 0x33
END
call CheckLegacyAndVim9Failure(lines, 'E979:')
endfunc

func Test_blob_for_loop()
Expand Down
16 changes: 13 additions & 3 deletions src/testdir/test_vim9_disassemble.vim
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ def s:ScriptFuncStoreMember()
locallist[0] = 123
var localdict: dict<number> = {}
localdict["a"] = 456
var localblob: blob = 0z1122
localblob[1] = 33
enddef

def Test_disassemble_store_member()
Expand All @@ -259,7 +261,7 @@ def Test_disassemble_store_member()
'\d PUSHNR 123\_s*' ..
'\d PUSHNR 0\_s*' ..
'\d LOAD $0\_s*' ..
'\d STORELIST\_s*' ..
'\d STOREINDEX list\_s*' ..
'var localdict: dict<number> = {}\_s*' ..
'\d NEWDICT size 0\_s*' ..
'\d SETTYPE dict<number>\_s*' ..
Expand All @@ -268,7 +270,15 @@ def Test_disassemble_store_member()
'\d\+ PUSHNR 456\_s*' ..
'\d\+ PUSHS "a"\_s*' ..
'\d\+ LOAD $1\_s*' ..
'\d\+ STOREDICT\_s*' ..
'\d\+ STOREINDEX dict\_s*' ..
'var localblob: blob = 0z1122\_s*' ..
'\d\+ PUSHBLOB 0z1122\_s*' ..
'\d\+ STORE $2\_s*' ..
'localblob\[1\] = 33\_s*' ..
'\d\+ PUSHNR 33\_s*' ..
'\d\+ PUSHNR 1\_s*' ..
'\d\+ LOAD $2\_s*' ..
'\d\+ STOREINDEX blob\_s*' ..
'\d\+ RETURN 0',
res)
enddef
Expand All @@ -291,7 +301,7 @@ def Test_disassemble_store_index()
'\d PUSHNR 0\_s*' ..
'\d LOAD $0\_s*' ..
'\d MEMBER dd\_s*' ..
'\d STOREINDEX\_s*' ..
'\d STOREINDEX any\_s*' ..
'\d\+ RETURN 0',
res)
enddef
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2777,
/**/
2776,
/**/
Expand Down
19 changes: 13 additions & 6 deletions src/vim9compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6209,14 +6209,21 @@ compile_assign_unlet(
}
if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
return FAIL;
if (dest_type == VAR_LIST)
if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
{
if (range
&& need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
&t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
type_T *type;

if (range)
{
type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
if (need_type(type, &t_number,
-1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
&t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
}
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if ((dest_type != VAR_BLOB || type != &t_special)
&& need_type(type, &t_number,
-1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
}
Expand Down
Loading

0 comments on commit 51e9332

Please sign in to comment.