| ... | ... | @@ -371,7 +371,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 371 | 371 | pub fn initCapacity(allocator: *Allocator, num: usize) !Self { |
| 372 | 372 | var self = Self{}; |
| 373 | 373 | |
| 374 | | const new_memory = try self.allocator.allocAdvanced(T, alignment, num, .at_least); |
| 374 | const new_memory = try allocator.allocAdvanced(T, alignment, num, .at_least); |
| 375 | 375 | self.items.ptr = new_memory.ptr; |
| 376 | 376 | self.capacity = new_memory.len; |
| 377 | 377 | |
| ... | ... | @@ -419,7 +419,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 419 | 419 | /// Replace range of elements `list[start..start+len]` with `new_items` |
| 420 | 420 | /// grows list if `len < new_items.len`. may allocate |
| 421 | 421 | /// shrinks list if `len > new_items.len` |
| 422 | | pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: SliceConst) !void { |
| 422 | pub fn replaceRange(self: *Self, allocator: *Allocator, start: usize, len: usize, new_items: SliceConst) !void { |
| 423 | 423 | var managed = self.toManaged(allocator); |
| 424 | 424 | try managed.replaceRange(start, len, new_items); |
| 425 | 425 | self.* = managed.toUnmanaged(); |
| ... | ... | @@ -617,201 +617,414 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 617 | 617 | }; |
| 618 | 618 | } |
| 619 | 619 | |
| 620 | | test "std.ArrayList.init" { |
| 621 | | var list = ArrayList(i32).init(testing.allocator); |
| 622 | | defer list.deinit(); |
| 620 | test "std.ArrayList/ArrayListUnmanaged.init" { |
| 621 | { |
| 622 | var list = ArrayList(i32).init(testing.allocator); |
| 623 | defer list.deinit(); |
| 623 | 624 | |
| 624 | | testing.expect(list.items.len == 0); |
| 625 | | testing.expect(list.capacity == 0); |
| 626 | | } |
| 625 | testing.expect(list.items.len == 0); |
| 626 | testing.expect(list.capacity == 0); |
| 627 | } |
| 627 | 628 | |
| 628 | | test "std.ArrayList.initCapacity" { |
| 629 | | var list = try ArrayList(i8).initCapacity(testing.allocator, 200); |
| 630 | | defer list.deinit(); |
| 631 | | testing.expect(list.items.len == 0); |
| 632 | | testing.expect(list.capacity >= 200); |
| 633 | | } |
| 629 | { |
| 630 | var list = ArrayListUnmanaged(i32){}; |
| 634 | 631 | |
| 635 | | test "std.ArrayList.basic" { |
| 636 | | var list = ArrayList(i32).init(testing.allocator); |
| 637 | | defer list.deinit(); |
| 632 | testing.expect(list.items.len == 0); |
| 633 | testing.expect(list.capacity == 0); |
| 634 | } |
| 635 | } |
| 638 | 636 | |
| 637 | test "std.ArrayList/ArrayListUnmanaged.initCapacity" { |
| 638 | const a = testing.allocator; |
| 639 | 639 | { |
| 640 | | var i: usize = 0; |
| 641 | | while (i < 10) : (i += 1) { |
| 642 | | list.append(@intCast(i32, i + 1)) catch unreachable; |
| 643 | | } |
| 640 | var list = try ArrayList(i8).initCapacity(a, 200); |
| 641 | defer list.deinit(); |
| 642 | testing.expect(list.items.len == 0); |
| 643 | testing.expect(list.capacity >= 200); |
| 644 | } |
| 645 | { |
| 646 | var list = try ArrayListUnmanaged(i8).initCapacity(a, 200); |
| 647 | defer list.deinit(a); |
| 648 | testing.expect(list.items.len == 0); |
| 649 | testing.expect(list.capacity >= 200); |
| 644 | 650 | } |
| 651 | } |
| 645 | 652 | |
| 653 | test "std.ArrayList/ArrayListUnmanaged.basic" { |
| 654 | const a = testing.allocator; |
| 646 | 655 | { |
| 647 | | var i: usize = 0; |
| 648 | | while (i < 10) : (i += 1) { |
| 649 | | testing.expect(list.items[i] == @intCast(i32, i + 1)); |
| 656 | var list = ArrayList(i32).init(a); |
| 657 | defer list.deinit(); |
| 658 | |
| 659 | { |
| 660 | var i: usize = 0; |
| 661 | while (i < 10) : (i += 1) { |
| 662 | list.append(@intCast(i32, i + 1)) catch unreachable; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | { |
| 667 | var i: usize = 0; |
| 668 | while (i < 10) : (i += 1) { |
| 669 | testing.expect(list.items[i] == @intCast(i32, i + 1)); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | for (list.items) |v, i| { |
| 674 | testing.expect(v == @intCast(i32, i + 1)); |
| 650 | 675 | } |
| 651 | | } |
| 652 | 676 | |
| 653 | | for (list.items) |v, i| { |
| 654 | | testing.expect(v == @intCast(i32, i + 1)); |
| 677 | testing.expect(list.pop() == 10); |
| 678 | testing.expect(list.items.len == 9); |
| 679 | |
| 680 | list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable; |
| 681 | testing.expect(list.items.len == 12); |
| 682 | testing.expect(list.pop() == 3); |
| 683 | testing.expect(list.pop() == 2); |
| 684 | testing.expect(list.pop() == 1); |
| 685 | testing.expect(list.items.len == 9); |
| 686 | |
| 687 | list.appendSlice(&[_]i32{}) catch unreachable; |
| 688 | testing.expect(list.items.len == 9); |
| 689 | |
| 690 | // can only set on indices < self.items.len |
| 691 | list.items[7] = 33; |
| 692 | list.items[8] = 42; |
| 693 | |
| 694 | testing.expect(list.pop() == 42); |
| 695 | testing.expect(list.pop() == 33); |
| 655 | 696 | } |
| 697 | { |
| 698 | var list = ArrayListUnmanaged(i32){}; |
| 699 | defer list.deinit(a); |
| 700 | |
| 701 | { |
| 702 | var i: usize = 0; |
| 703 | while (i < 10) : (i += 1) { |
| 704 | list.append(a, @intCast(i32, i + 1)) catch unreachable; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | { |
| 709 | var i: usize = 0; |
| 710 | while (i < 10) : (i += 1) { |
| 711 | testing.expect(list.items[i] == @intCast(i32, i + 1)); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | for (list.items) |v, i| { |
| 716 | testing.expect(v == @intCast(i32, i + 1)); |
| 717 | } |
| 656 | 718 | |
| 657 | | testing.expect(list.pop() == 10); |
| 658 | | testing.expect(list.items.len == 9); |
| 719 | testing.expect(list.pop() == 10); |
| 720 | testing.expect(list.items.len == 9); |
| 659 | 721 | |
| 660 | | list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable; |
| 661 | | testing.expect(list.items.len == 12); |
| 662 | | testing.expect(list.pop() == 3); |
| 663 | | testing.expect(list.pop() == 2); |
| 664 | | testing.expect(list.pop() == 1); |
| 665 | | testing.expect(list.items.len == 9); |
| 722 | list.appendSlice(a, &[_]i32{ 1, 2, 3 }) catch unreachable; |
| 723 | testing.expect(list.items.len == 12); |
| 724 | testing.expect(list.pop() == 3); |
| 725 | testing.expect(list.pop() == 2); |
| 726 | testing.expect(list.pop() == 1); |
| 727 | testing.expect(list.items.len == 9); |
| 666 | 728 | |
| 667 | | list.appendSlice(&[_]i32{}) catch unreachable; |
| 668 | | testing.expect(list.items.len == 9); |
| 729 | list.appendSlice(a, &[_]i32{}) catch unreachable; |
| 730 | testing.expect(list.items.len == 9); |
| 669 | 731 | |
| 670 | | // can only set on indices < self.items.len |
| 671 | | list.items[7] = 33; |
| 672 | | list.items[8] = 42; |
| 732 | // can only set on indices < self.items.len |
| 733 | list.items[7] = 33; |
| 734 | list.items[8] = 42; |
| 673 | 735 | |
| 674 | | testing.expect(list.pop() == 42); |
| 675 | | testing.expect(list.pop() == 33); |
| 736 | testing.expect(list.pop() == 42); |
| 737 | testing.expect(list.pop() == 33); |
| 738 | } |
| 676 | 739 | } |
| 677 | 740 | |
| 678 | | test "std.ArrayList.appendNTimes" { |
| 679 | | var list = ArrayList(i32).init(testing.allocator); |
| 680 | | defer list.deinit(); |
| 741 | test "std.ArrayList/ArrayListUnmanaged.appendNTimes" { |
| 742 | const a = testing.allocator; |
| 743 | { |
| 744 | var list = ArrayList(i32).init(a); |
| 745 | defer list.deinit(); |
| 746 | |
| 747 | try list.appendNTimes(2, 10); |
| 748 | testing.expectEqual(@as(usize, 10), list.items.len); |
| 749 | for (list.items) |element| { |
| 750 | testing.expectEqual(@as(i32, 2), element); |
| 751 | } |
| 752 | } |
| 753 | { |
| 754 | var list = ArrayListUnmanaged(i32){}; |
| 755 | defer list.deinit(a); |
| 681 | 756 | |
| 682 | | try list.appendNTimes(2, 10); |
| 683 | | testing.expectEqual(@as(usize, 10), list.items.len); |
| 684 | | for (list.items) |element| { |
| 685 | | testing.expectEqual(@as(i32, 2), element); |
| 757 | try list.appendNTimes(a, 2, 10); |
| 758 | testing.expectEqual(@as(usize, 10), list.items.len); |
| 759 | for (list.items) |element| { |
| 760 | testing.expectEqual(@as(i32, 2), element); |
| 761 | } |
| 686 | 762 | } |
| 687 | 763 | } |
| 688 | 764 | |
| 689 | | test "std.ArrayList.appendNTimes with failing allocator" { |
| 690 | | var list = ArrayList(i32).init(testing.failing_allocator); |
| 691 | | defer list.deinit(); |
| 692 | | testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10)); |
| 765 | test "std.ArrayList/ArrayListUnmanaged.appendNTimes with failing allocator" { |
| 766 | const a = testing.failing_allocator; |
| 767 | { |
| 768 | var list = ArrayList(i32).init(a); |
| 769 | defer list.deinit(); |
| 770 | testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10)); |
| 771 | } |
| 772 | { |
| 773 | var list = ArrayListUnmanaged(i32){}; |
| 774 | defer list.deinit(a); |
| 775 | testing.expectError(error.OutOfMemory, list.appendNTimes(a, 2, 10)); |
| 776 | } |
| 693 | 777 | } |
| 694 | 778 | |
| 695 | | test "std.ArrayList.orderedRemove" { |
| 696 | | var list = ArrayList(i32).init(testing.allocator); |
| 697 | | defer list.deinit(); |
| 779 | test "std.ArrayList/ArrayListUnmanaged.orderedRemove" { |
| 780 | const a = testing.allocator; |
| 781 | { |
| 782 | var list = ArrayList(i32).init(a); |
| 783 | defer list.deinit(); |
| 784 | |
| 785 | try list.append(1); |
| 786 | try list.append(2); |
| 787 | try list.append(3); |
| 788 | try list.append(4); |
| 789 | try list.append(5); |
| 790 | try list.append(6); |
| 791 | try list.append(7); |
| 792 | |
| 793 | //remove from middle |
| 794 | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); |
| 795 | testing.expectEqual(@as(i32, 5), list.items[3]); |
| 796 | testing.expectEqual(@as(usize, 6), list.items.len); |
| 797 | |
| 798 | //remove from end |
| 799 | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); |
| 800 | testing.expectEqual(@as(usize, 5), list.items.len); |
| 801 | |
| 802 | //remove from front |
| 803 | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); |
| 804 | testing.expectEqual(@as(i32, 2), list.items[0]); |
| 805 | testing.expectEqual(@as(usize, 4), list.items.len); |
| 806 | } |
| 807 | { |
| 808 | var list = ArrayListUnmanaged(i32){}; |
| 809 | defer list.deinit(a); |
| 698 | 810 | |
| 699 | | try list.append(1); |
| 700 | | try list.append(2); |
| 701 | | try list.append(3); |
| 702 | | try list.append(4); |
| 703 | | try list.append(5); |
| 704 | | try list.append(6); |
| 705 | | try list.append(7); |
| 706 | | |
| 707 | | //remove from middle |
| 708 | | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); |
| 709 | | testing.expectEqual(@as(i32, 5), list.items[3]); |
| 710 | | testing.expectEqual(@as(usize, 6), list.items.len); |
| 711 | | |
| 712 | | //remove from end |
| 713 | | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); |
| 714 | | testing.expectEqual(@as(usize, 5), list.items.len); |
| 715 | | |
| 716 | | //remove from front |
| 717 | | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); |
| 718 | | testing.expectEqual(@as(i32, 2), list.items[0]); |
| 719 | | testing.expectEqual(@as(usize, 4), list.items.len); |
| 811 | try list.append(a, 1); |
| 812 | try list.append(a, 2); |
| 813 | try list.append(a, 3); |
| 814 | try list.append(a, 4); |
| 815 | try list.append(a, 5); |
| 816 | try list.append(a, 6); |
| 817 | try list.append(a, 7); |
| 818 | |
| 819 | //remove from middle |
| 820 | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); |
| 821 | testing.expectEqual(@as(i32, 5), list.items[3]); |
| 822 | testing.expectEqual(@as(usize, 6), list.items.len); |
| 823 | |
| 824 | //remove from end |
| 825 | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); |
| 826 | testing.expectEqual(@as(usize, 5), list.items.len); |
| 827 | |
| 828 | //remove from front |
| 829 | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); |
| 830 | testing.expectEqual(@as(i32, 2), list.items[0]); |
| 831 | testing.expectEqual(@as(usize, 4), list.items.len); |
| 832 | } |
| 720 | 833 | } |
| 721 | 834 | |
| 722 | | test "std.ArrayList.swapRemove" { |
| 723 | | var list = ArrayList(i32).init(testing.allocator); |
| 724 | | defer list.deinit(); |
| 835 | test "std.ArrayList/ArrayListUnmanaged.swapRemove" { |
| 836 | const a = testing.allocator; |
| 837 | { |
| 838 | var list = ArrayList(i32).init(a); |
| 839 | defer list.deinit(); |
| 725 | 840 | |
| 726 | | try list.append(1); |
| 727 | | try list.append(2); |
| 728 | | try list.append(3); |
| 729 | | try list.append(4); |
| 730 | | try list.append(5); |
| 731 | | try list.append(6); |
| 732 | | try list.append(7); |
| 733 | | |
| 734 | | //remove from middle |
| 735 | | testing.expect(list.swapRemove(3) == 4); |
| 736 | | testing.expect(list.items[3] == 7); |
| 737 | | testing.expect(list.items.len == 6); |
| 738 | | |
| 739 | | //remove from end |
| 740 | | testing.expect(list.swapRemove(5) == 6); |
| 741 | | testing.expect(list.items.len == 5); |
| 742 | | |
| 743 | | //remove from front |
| 744 | | testing.expect(list.swapRemove(0) == 1); |
| 745 | | testing.expect(list.items[0] == 5); |
| 746 | | testing.expect(list.items.len == 4); |
| 841 | try list.append(1); |
| 842 | try list.append(2); |
| 843 | try list.append(3); |
| 844 | try list.append(4); |
| 845 | try list.append(5); |
| 846 | try list.append(6); |
| 847 | try list.append(7); |
| 848 | |
| 849 | //remove from middle |
| 850 | testing.expect(list.swapRemove(3) == 4); |
| 851 | testing.expect(list.items[3] == 7); |
| 852 | testing.expect(list.items.len == 6); |
| 853 | |
| 854 | //remove from end |
| 855 | testing.expect(list.swapRemove(5) == 6); |
| 856 | testing.expect(list.items.len == 5); |
| 857 | |
| 858 | //remove from front |
| 859 | testing.expect(list.swapRemove(0) == 1); |
| 860 | testing.expect(list.items[0] == 5); |
| 861 | testing.expect(list.items.len == 4); |
| 862 | } |
| 863 | { |
| 864 | var list = ArrayListUnmanaged(i32){}; |
| 865 | defer list.deinit(a); |
| 866 | |
| 867 | try list.append(a, 1); |
| 868 | try list.append(a, 2); |
| 869 | try list.append(a, 3); |
| 870 | try list.append(a, 4); |
| 871 | try list.append(a, 5); |
| 872 | try list.append(a, 6); |
| 873 | try list.append(a, 7); |
| 874 | |
| 875 | //remove from middle |
| 876 | testing.expect(list.swapRemove(3) == 4); |
| 877 | testing.expect(list.items[3] == 7); |
| 878 | testing.expect(list.items.len == 6); |
| 879 | |
| 880 | //remove from end |
| 881 | testing.expect(list.swapRemove(5) == 6); |
| 882 | testing.expect(list.items.len == 5); |
| 883 | |
| 884 | //remove from front |
| 885 | testing.expect(list.swapRemove(0) == 1); |
| 886 | testing.expect(list.items[0] == 5); |
| 887 | testing.expect(list.items.len == 4); |
| 888 | } |
| 747 | 889 | } |
| 748 | 890 | |
| 749 | | test "std.ArrayList.insert" { |
| 750 | | var list = ArrayList(i32).init(testing.allocator); |
| 751 | | defer list.deinit(); |
| 891 | test "std.ArrayList/ArrayListUnmanaged.insert" { |
| 892 | const a = testing.allocator; |
| 893 | { |
| 894 | var list = ArrayList(i32).init(a); |
| 895 | defer list.deinit(); |
| 752 | 896 | |
| 753 | | try list.append(1); |
| 754 | | try list.append(2); |
| 755 | | try list.append(3); |
| 756 | | try list.insert(0, 5); |
| 757 | | testing.expect(list.items[0] == 5); |
| 758 | | testing.expect(list.items[1] == 1); |
| 759 | | testing.expect(list.items[2] == 2); |
| 760 | | testing.expect(list.items[3] == 3); |
| 897 | try list.append(1); |
| 898 | try list.append(2); |
| 899 | try list.append(3); |
| 900 | try list.insert(0, 5); |
| 901 | testing.expect(list.items[0] == 5); |
| 902 | testing.expect(list.items[1] == 1); |
| 903 | testing.expect(list.items[2] == 2); |
| 904 | testing.expect(list.items[3] == 3); |
| 905 | } |
| 906 | { |
| 907 | var list = ArrayListUnmanaged(i32){}; |
| 908 | defer list.deinit(a); |
| 909 | |
| 910 | try list.append(a, 1); |
| 911 | try list.append(a, 2); |
| 912 | try list.append(a, 3); |
| 913 | try list.insert(a, 0, 5); |
| 914 | testing.expect(list.items[0] == 5); |
| 915 | testing.expect(list.items[1] == 1); |
| 916 | testing.expect(list.items[2] == 2); |
| 917 | testing.expect(list.items[3] == 3); |
| 918 | } |
| 761 | 919 | } |
| 762 | 920 | |
| 763 | | test "std.ArrayList.insertSlice" { |
| 764 | | var list = ArrayList(i32).init(testing.allocator); |
| 765 | | defer list.deinit(); |
| 921 | test "std.ArrayList/ArrayListUnmanaged.insertSlice" { |
| 922 | const a = testing.allocator; |
| 923 | { |
| 924 | var list = ArrayList(i32).init(a); |
| 925 | defer list.deinit(); |
| 766 | 926 | |
| 767 | | try list.append(1); |
| 768 | | try list.append(2); |
| 769 | | try list.append(3); |
| 770 | | try list.append(4); |
| 771 | | try list.insertSlice(1, &[_]i32{ 9, 8 }); |
| 772 | | testing.expect(list.items[0] == 1); |
| 773 | | testing.expect(list.items[1] == 9); |
| 774 | | testing.expect(list.items[2] == 8); |
| 775 | | testing.expect(list.items[3] == 2); |
| 776 | | testing.expect(list.items[4] == 3); |
| 777 | | testing.expect(list.items[5] == 4); |
| 778 | | |
| 779 | | const items = [_]i32{1}; |
| 780 | | try list.insertSlice(0, items[0..0]); |
| 781 | | testing.expect(list.items.len == 6); |
| 782 | | testing.expect(list.items[0] == 1); |
| 927 | try list.append(1); |
| 928 | try list.append(2); |
| 929 | try list.append(3); |
| 930 | try list.append(4); |
| 931 | try list.insertSlice(1, &[_]i32{ 9, 8 }); |
| 932 | testing.expect(list.items[0] == 1); |
| 933 | testing.expect(list.items[1] == 9); |
| 934 | testing.expect(list.items[2] == 8); |
| 935 | testing.expect(list.items[3] == 2); |
| 936 | testing.expect(list.items[4] == 3); |
| 937 | testing.expect(list.items[5] == 4); |
| 938 | |
| 939 | const items = [_]i32{1}; |
| 940 | try list.insertSlice(0, items[0..0]); |
| 941 | testing.expect(list.items.len == 6); |
| 942 | testing.expect(list.items[0] == 1); |
| 943 | } |
| 944 | { |
| 945 | var list = ArrayListUnmanaged(i32){}; |
| 946 | defer list.deinit(a); |
| 947 | |
| 948 | try list.append(a, 1); |
| 949 | try list.append(a, 2); |
| 950 | try list.append(a, 3); |
| 951 | try list.append(a, 4); |
| 952 | try list.insertSlice(a, 1, &[_]i32{ 9, 8 }); |
| 953 | testing.expect(list.items[0] == 1); |
| 954 | testing.expect(list.items[1] == 9); |
| 955 | testing.expect(list.items[2] == 8); |
| 956 | testing.expect(list.items[3] == 2); |
| 957 | testing.expect(list.items[4] == 3); |
| 958 | testing.expect(list.items[5] == 4); |
| 959 | |
| 960 | const items = [_]i32{1}; |
| 961 | try list.insertSlice(a, 0, items[0..0]); |
| 962 | testing.expect(list.items.len == 6); |
| 963 | testing.expect(list.items[0] == 1); |
| 964 | } |
| 783 | 965 | } |
| 784 | 966 | |
| 785 | | test "std.ArrayList.replaceRange" { |
| 967 | test "std.ArrayList/ArrayListUnmanaged.replaceRange" { |
| 786 | 968 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 787 | 969 | defer arena.deinit(); |
| 970 | const a = &arena.allocator; |
| 788 | 971 | |
| 789 | | const alloc = &arena.allocator; |
| 790 | 972 | const init = [_]i32{ 1, 2, 3, 4, 5 }; |
| 791 | 973 | const new = [_]i32{ 0, 0, 0 }; |
| 792 | 974 | |
| 793 | | var list_zero = ArrayList(i32).init(alloc); |
| 794 | | var list_eq = ArrayList(i32).init(alloc); |
| 795 | | var list_lt = ArrayList(i32).init(alloc); |
| 796 | | var list_gt = ArrayList(i32).init(alloc); |
| 975 | const result_zero = [_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }; |
| 976 | const result_eq = [_]i32{ 1, 0, 0, 0, 5 }; |
| 977 | const result_le = [_]i32{ 1, 0, 0, 0, 4, 5 }; |
| 978 | const result_gt = [_]i32{ 1, 0, 0, 0 }; |
| 797 | 979 | |
| 798 | | try list_zero.appendSlice(&init); |
| 799 | | try list_eq.appendSlice(&init); |
| 800 | | try list_lt.appendSlice(&init); |
| 801 | | try list_gt.appendSlice(&init); |
| 802 | | |
| 803 | | try list_zero.replaceRange(1, 0, &new); |
| 804 | | try list_eq.replaceRange(1, 3, &new); |
| 805 | | try list_lt.replaceRange(1, 2, &new); |
| 806 | | |
| 807 | | // after_range > new_items.len in function body |
| 808 | | testing.expect(1 + 4 > new.len); |
| 809 | | try list_gt.replaceRange(1, 4, &new); |
| 810 | | |
| 811 | | testing.expectEqualSlices(i32, list_zero.items, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }); |
| 812 | | testing.expectEqualSlices(i32, list_eq.items, &[_]i32{ 1, 0, 0, 0, 5 }); |
| 813 | | testing.expectEqualSlices(i32, list_lt.items, &[_]i32{ 1, 0, 0, 0, 4, 5 }); |
| 814 | | testing.expectEqualSlices(i32, list_gt.items, &[_]i32{ 1, 0, 0, 0 }); |
| 980 | { |
| 981 | var list_zero = ArrayList(i32).init(a); |
| 982 | var list_eq = ArrayList(i32).init(a); |
| 983 | var list_lt = ArrayList(i32).init(a); |
| 984 | var list_gt = ArrayList(i32).init(a); |
| 985 | |
| 986 | try list_zero.appendSlice(&init); |
| 987 | try list_eq.appendSlice(&init); |
| 988 | try list_lt.appendSlice(&init); |
| 989 | try list_gt.appendSlice(&init); |
| 990 | |
| 991 | try list_zero.replaceRange(1, 0, &new); |
| 992 | try list_eq.replaceRange(1, 3, &new); |
| 993 | try list_lt.replaceRange(1, 2, &new); |
| 994 | |
| 995 | // after_range > new_items.len in function body |
| 996 | testing.expect(1 + 4 > new.len); |
| 997 | try list_gt.replaceRange(1, 4, &new); |
| 998 | |
| 999 | testing.expectEqualSlices(i32, list_zero.items, &result_zero); |
| 1000 | testing.expectEqualSlices(i32, list_eq.items, &result_eq); |
| 1001 | testing.expectEqualSlices(i32, list_lt.items, &result_le); |
| 1002 | testing.expectEqualSlices(i32, list_gt.items, &result_gt); |
| 1003 | } |
| 1004 | { |
| 1005 | var list_zero = ArrayListUnmanaged(i32){}; |
| 1006 | var list_eq = ArrayListUnmanaged(i32){}; |
| 1007 | var list_lt = ArrayListUnmanaged(i32){}; |
| 1008 | var list_gt = ArrayListUnmanaged(i32){}; |
| 1009 | |
| 1010 | try list_zero.appendSlice(a, &init); |
| 1011 | try list_eq.appendSlice(a, &init); |
| 1012 | try list_lt.appendSlice(a, &init); |
| 1013 | try list_gt.appendSlice(a, &init); |
| 1014 | |
| 1015 | try list_zero.replaceRange(a, 1, 0, &new); |
| 1016 | try list_eq.replaceRange(a, 1, 3, &new); |
| 1017 | try list_lt.replaceRange(a, 1, 2, &new); |
| 1018 | |
| 1019 | // after_range > new_items.len in function body |
| 1020 | testing.expect(1 + 4 > new.len); |
| 1021 | try list_gt.replaceRange(a, 1, 4, &new); |
| 1022 | |
| 1023 | testing.expectEqualSlices(i32, list_zero.items, &result_zero); |
| 1024 | testing.expectEqualSlices(i32, list_eq.items, &result_eq); |
| 1025 | testing.expectEqualSlices(i32, list_lt.items, &result_le); |
| 1026 | testing.expectEqualSlices(i32, list_gt.items, &result_gt); |
| 1027 | } |
| 815 | 1028 | } |
| 816 | 1029 | |
| 817 | 1030 | const Item = struct { |
| ... | ... | @@ -819,11 +1032,25 @@ const Item = struct { |
| 819 | 1032 | sub_items: ArrayList(Item), |
| 820 | 1033 | }; |
| 821 | 1034 | |
| 822 | | test "std.ArrayList: ArrayList(T) of struct T" { |
| 823 | | var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(testing.allocator) }; |
| 824 | | defer root.sub_items.deinit(); |
| 825 | | try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) }); |
| 826 | | testing.expect(root.sub_items.items[0].integer == 42); |
| 1035 | const ItemUnmanaged = struct { |
| 1036 | integer: i32, |
| 1037 | sub_items: ArrayListUnmanaged(ItemUnmanaged), |
| 1038 | }; |
| 1039 | |
| 1040 | test "std.ArrayList/ArrayListUnmanaged: ArrayList(T) of struct T" { |
| 1041 | const a = std.testing.allocator; |
| 1042 | { |
| 1043 | var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(a) }; |
| 1044 | defer root.sub_items.deinit(); |
| 1045 | try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(a) }); |
| 1046 | testing.expect(root.sub_items.items[0].integer == 42); |
| 1047 | } |
| 1048 | { |
| 1049 | var root = ItemUnmanaged{ .integer = 1, .sub_items = ArrayListUnmanaged(ItemUnmanaged){} }; |
| 1050 | defer root.sub_items.deinit(a); |
| 1051 | try root.sub_items.append(a, ItemUnmanaged{ .integer = 42, .sub_items = ArrayListUnmanaged(ItemUnmanaged){} }); |
| 1052 | testing.expect(root.sub_items.items[0].integer == 42); |
| 1053 | } |
| 827 | 1054 | } |
| 828 | 1055 | |
| 829 | 1056 | test "std.ArrayList(u8) implements outStream" { |
| ... | ... | @@ -837,19 +1064,32 @@ test "std.ArrayList(u8) implements outStream" { |
| 837 | 1064 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span()); |
| 838 | 1065 | } |
| 839 | 1066 | |
| 840 | | test "std.ArrayList.shrink still sets length on error.OutOfMemory" { |
| 1067 | test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMemory" { |
| 841 | 1068 | // use an arena allocator to make sure realloc returns error.OutOfMemory |
| 842 | 1069 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 843 | 1070 | defer arena.deinit(); |
| 1071 | const a = &arena.allocator; |
| 844 | 1072 | |
| 845 | | var list = ArrayList(i32).init(&arena.allocator); |
| 1073 | { |
| 1074 | var list = ArrayList(i32).init(a); |
| 846 | 1075 | |
| 847 | | try list.append(1); |
| 848 | | try list.append(2); |
| 849 | | try list.append(3); |
| 1076 | try list.append(1); |
| 1077 | try list.append(2); |
| 1078 | try list.append(3); |
| 850 | 1079 | |
| 851 | | list.shrink(1); |
| 852 | | testing.expect(list.items.len == 1); |
| 1080 | list.shrink(1); |
| 1081 | testing.expect(list.items.len == 1); |
| 1082 | } |
| 1083 | { |
| 1084 | var list = ArrayListUnmanaged(i32){}; |
| 1085 | |
| 1086 | try list.append(a, 1); |
| 1087 | try list.append(a, 2); |
| 1088 | try list.append(a, 3); |
| 1089 | |
| 1090 | list.shrink(a, 1); |
| 1091 | testing.expect(list.items.len == 1); |
| 1092 | } |
| 853 | 1093 | } |
| 854 | 1094 | |
| 855 | 1095 | test "std.ArrayList.writer" { |
| ... | ... | @@ -864,7 +1104,7 @@ test "std.ArrayList.writer" { |
| 864 | 1104 | testing.expectEqualSlices(u8, list.items, "abcdefg"); |
| 865 | 1105 | } |
| 866 | 1106 | |
| 867 | | test "addManyAsArray" { |
| 1107 | test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" { |
| 868 | 1108 | const a = std.testing.allocator; |
| 869 | 1109 | { |
| 870 | 1110 | var list = ArrayList(u8).init(a); |