Closed saikishore222 closed 1 year ago
This is the project link ---> Shared Project
The problem is while accessing the whole array, the test was passing, but only when I tried to access the 0th index it wasn't working.
Ah I think I see what's going on here! print(Game_data.moves[0])
is compiling to:
solana_program::msg!(
"{}",
Game_data.borrow().moves.borrow()[Game_data.borrow().moves.wrapped_index(0)]
);
wrapped_index
gives us pythonic indexing, so stuff like moves[-1]
works:
pub fn wrapped_index(&self, mut index: i128) -> usize {
if index > 0 {
return index.try_into().unwrap();
}
index += self.borrow().len() as i128;
return index.try_into().unwrap();
}
When we call this with index: 0
the index > 0
test doesn't pass, so we add the length of the list (9) to it. That gives an index of 9, which fails. The reason we do that is if it's like -1
then you add 9 to get 8 and that gives you the nice negative indexing support. But for 0 we should be using the first branch that just uses the index as-is.
I'm pretty sure the fix is just to change that index to >= 0
but I'll need to write a TS test to verify it works. You don't have any TS tests yet by any chance?
If you change your code to use moves[1]
and [moves[-1]
then both work: https://explorer.solana.com/tx/4y6gXeUXZcKgVFneVctnmX97S5WZrpi8RpAS89JFEiCtnGaPzbtPmbzM6iBxWtpyJG3ncNLy7e1YAkEaic7XraSo?cluster=devnet
So I think it's just a problem with the 0 index.
I tried with 0 index in different ways. it has a problem with 0 index, thanks @mcintyre94
Wow I made an off-by-one error, that's embarassing. Thanks @saikishore222 for finding this and @mcintyre94 for finding the fix!
when will the next build release, use array index by 0th position? @mcintyre94 @ameliatastic
Array Accessing by Index was got the error , test was failed