| ... | @@ -371,7 +371,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -371,7 +371,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 371 | pub fn initCapacity(allocator: *Allocator, num: usize) !Self { | 371 | pub fn initCapacity(allocator: *Allocator, num: usize) !Self { |
| 372 | var self = Self{}; | 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 | self.items.ptr = new_memory.ptr; | 375 | self.items.ptr = new_memory.ptr; |
| 376 | self.capacity = new_memory.len; | 376 | self.capacity = new_memory.len; |
| 377 | | 377 | |
| ... | @@ -419,7 +419,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -419,7 +419,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 419 | /// Replace range of elements `list[start..start+len]` with `new_items` | 419 | /// Replace range of elements `list[start..start+len]` with `new_items` |
| 420 | /// grows list if `len < new_items.len`. may allocate | 420 | /// grows list if `len < new_items.len`. may allocate |
| 421 | /// shrinks list if `len > new_items.len` | 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 | var managed = self.toManaged(allocator); | 423 | var managed = self.toManaged(allocator); |
| 424 | try managed.replaceRange(start, len, new_items); | 424 | try managed.replaceRange(start, len, new_items); |
| 425 | self.* = managed.toUnmanaged(); | 425 | self.* = managed.toUnmanaged(); |
| ... | @@ -617,201 +617,414 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -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" { | 620 | test "std.ArrayList/ArrayListUnmanaged.init" { |
| 621 | var list = ArrayList(i32).init(testing.allocator); | 621 | { |
| 622 | defer list.deinit(); | 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.items.len == 0); |
| 625 | testing.expect(list.capacity == 0); | 626 | testing.expect(list.capacity == 0); |
| 626 | } | 627 | } |
| 627 | | 628 | |
| 628 | test "std.ArrayList.initCapacity" { | 629 | { |
| 629 | var list = try ArrayList(i8).initCapacity(testing.allocator, 200); | 630 | var list = ArrayListUnmanaged(i32){}; |
| 630 | defer list.deinit(); | | |
| 631 | testing.expect(list.items.len == 0); | | |
| 632 | testing.expect(list.capacity >= 200); | | |
| 633 | } | | |
| 634 | | 631 | |
| 635 | test "std.ArrayList.basic" { | 632 | testing.expect(list.items.len == 0); |
| 636 | var list = ArrayList(i32).init(testing.allocator); | 633 | testing.expect(list.capacity == 0); |
| 637 | defer list.deinit(); | 634 | } |
| | 635 | } |
| 638 | | 636 | |
| | 637 | test "std.ArrayList/ArrayListUnmanaged.initCapacity" { |
| | 638 | const a = testing.allocator; |
| 639 | { | 639 | { |
| 640 | var i: usize = 0; | 640 | var list = try ArrayList(i8).initCapacity(a, 200); |
| 641 | while (i < 10) : (i += 1) { | 641 | defer list.deinit(); |
| 642 | list.append(@intCast(i32, i + 1)) catch unreachable; | 642 | testing.expect(list.items.len == 0); |
| 643 | } | 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; | 656 | var list = ArrayList(i32).init(a); |
| 648 | while (i < 10) : (i += 1) { | 657 | defer list.deinit(); |
| 649 | testing.expect(list.items[i] == @intCast(i32, i + 1)); | 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| { | 677 | testing.expect(list.pop() == 10); |
| 654 | testing.expect(v == @intCast(i32, i + 1)); | 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); | 719 | testing.expect(list.pop() == 10); |
| 658 | testing.expect(list.items.len == 9); | 720 | testing.expect(list.items.len == 9); |
| 659 | | 721 | |
| 660 | list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable; | 722 | list.appendSlice(a, &[_]i32{ 1, 2, 3 }) catch unreachable; |
| 661 | testing.expect(list.items.len == 12); | 723 | testing.expect(list.items.len == 12); |
| 662 | testing.expect(list.pop() == 3); | 724 | testing.expect(list.pop() == 3); |
| 663 | testing.expect(list.pop() == 2); | 725 | testing.expect(list.pop() == 2); |
| 664 | testing.expect(list.pop() == 1); | 726 | testing.expect(list.pop() == 1); |
| 665 | testing.expect(list.items.len == 9); | 727 | testing.expect(list.items.len == 9); |
| 666 | | 728 | |
| 667 | list.appendSlice(&[_]i32{}) catch unreachable; | 729 | list.appendSlice(a, &[_]i32{}) catch unreachable; |
| 668 | testing.expect(list.items.len == 9); | 730 | testing.expect(list.items.len == 9); |
| 669 | | 731 | |
| 670 | // can only set on indices < self.items.len | 732 | // can only set on indices < self.items.len |
| 671 | list.items[7] = 33; | 733 | list.items[7] = 33; |
| 672 | list.items[8] = 42; | 734 | list.items[8] = 42; |
| 673 | | 735 | |
| 674 | testing.expect(list.pop() == 42); | 736 | testing.expect(list.pop() == 42); |
| 675 | testing.expect(list.pop() == 33); | 737 | testing.expect(list.pop() == 33); |
| | 738 | } |
| 676 | } | 739 | } |
| 677 | | 740 | |
| 678 | test "std.ArrayList.appendNTimes" { | 741 | test "std.ArrayList/ArrayListUnmanaged.appendNTimes" { |
| 679 | var list = ArrayList(i32).init(testing.allocator); | 742 | const a = testing.allocator; |
| 680 | defer list.deinit(); | 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); | 757 | try list.appendNTimes(a, 2, 10); |
| 683 | testing.expectEqual(@as(usize, 10), list.items.len); | 758 | testing.expectEqual(@as(usize, 10), list.items.len); |
| 684 | for (list.items) |element| { | 759 | for (list.items) |element| { |
| 685 | testing.expectEqual(@as(i32, 2), element); | 760 | testing.expectEqual(@as(i32, 2), element); |
| | 761 | } |
| 686 | } | 762 | } |
| 687 | } | 763 | } |
| 688 | | 764 | |
| 689 | test "std.ArrayList.appendNTimes with failing allocator" { | 765 | test "std.ArrayList/ArrayListUnmanaged.appendNTimes with failing allocator" { |
| 690 | var list = ArrayList(i32).init(testing.failing_allocator); | 766 | const a = testing.failing_allocator; |
| 691 | defer list.deinit(); | 767 | { |
| 692 | testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10)); | 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" { | 779 | test "std.ArrayList/ArrayListUnmanaged.orderedRemove" { |
| 696 | var list = ArrayList(i32).init(testing.allocator); | 780 | const a = testing.allocator; |
| 697 | defer list.deinit(); | 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); | 811 | try list.append(a, 1); |
| 700 | try list.append(2); | 812 | try list.append(a, 2); |
| 701 | try list.append(3); | 813 | try list.append(a, 3); |
| 702 | try list.append(4); | 814 | try list.append(a, 4); |
| 703 | try list.append(5); | 815 | try list.append(a, 5); |
| 704 | try list.append(6); | 816 | try list.append(a, 6); |
| 705 | try list.append(7); | 817 | try list.append(a, 7); |
| 706 | | 818 | |
| 707 | //remove from middle | 819 | //remove from middle |
| 708 | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); | 820 | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); |
| 709 | testing.expectEqual(@as(i32, 5), list.items[3]); | 821 | testing.expectEqual(@as(i32, 5), list.items[3]); |
| 710 | testing.expectEqual(@as(usize, 6), list.items.len); | 822 | testing.expectEqual(@as(usize, 6), list.items.len); |
| 711 | | 823 | |
| 712 | //remove from end | 824 | //remove from end |
| 713 | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); | 825 | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); |
| 714 | testing.expectEqual(@as(usize, 5), list.items.len); | 826 | testing.expectEqual(@as(usize, 5), list.items.len); |
| 715 | | 827 | |
| 716 | //remove from front | 828 | //remove from front |
| 717 | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); | 829 | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); |
| 718 | testing.expectEqual(@as(i32, 2), list.items[0]); | 830 | testing.expectEqual(@as(i32, 2), list.items[0]); |
| 719 | testing.expectEqual(@as(usize, 4), list.items.len); | 831 | testing.expectEqual(@as(usize, 4), list.items.len); |
| | 832 | } |
| 720 | } | 833 | } |
| 721 | | 834 | |
| 722 | test "std.ArrayList.swapRemove" { | 835 | test "std.ArrayList/ArrayListUnmanaged.swapRemove" { |
| 723 | var list = ArrayList(i32).init(testing.allocator); | 836 | const a = testing.allocator; |
| 724 | defer list.deinit(); | 837 | { |
| | 838 | var list = ArrayList(i32).init(a); |
| | 839 | defer list.deinit(); |
| 725 | | 840 | |
| 726 | try list.append(1); | 841 | try list.append(1); |
| 727 | try list.append(2); | 842 | try list.append(2); |
| 728 | try list.append(3); | 843 | try list.append(3); |
| 729 | try list.append(4); | 844 | try list.append(4); |
| 730 | try list.append(5); | 845 | try list.append(5); |
| 731 | try list.append(6); | 846 | try list.append(6); |
| 732 | try list.append(7); | 847 | try list.append(7); |
| 733 | | 848 | |
| 734 | //remove from middle | 849 | //remove from middle |
| 735 | testing.expect(list.swapRemove(3) == 4); | 850 | testing.expect(list.swapRemove(3) == 4); |
| 736 | testing.expect(list.items[3] == 7); | 851 | testing.expect(list.items[3] == 7); |
| 737 | testing.expect(list.items.len == 6); | 852 | testing.expect(list.items.len == 6); |
| 738 | | 853 | |
| 739 | //remove from end | 854 | //remove from end |
| 740 | testing.expect(list.swapRemove(5) == 6); | 855 | testing.expect(list.swapRemove(5) == 6); |
| 741 | testing.expect(list.items.len == 5); | 856 | testing.expect(list.items.len == 5); |
| 742 | | 857 | |
| 743 | //remove from front | 858 | //remove from front |
| 744 | testing.expect(list.swapRemove(0) == 1); | 859 | testing.expect(list.swapRemove(0) == 1); |
| 745 | testing.expect(list.items[0] == 5); | 860 | testing.expect(list.items[0] == 5); |
| 746 | testing.expect(list.items.len == 4); | 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" { | 891 | test "std.ArrayList/ArrayListUnmanaged.insert" { |
| 750 | var list = ArrayList(i32).init(testing.allocator); | 892 | const a = testing.allocator; |
| 751 | defer list.deinit(); | 893 | { |
| | 894 | var list = ArrayList(i32).init(a); |
| | 895 | defer list.deinit(); |
| 752 | | 896 | |
| 753 | try list.append(1); | 897 | try list.append(1); |
| 754 | try list.append(2); | 898 | try list.append(2); |
| 755 | try list.append(3); | 899 | try list.append(3); |
| 756 | try list.insert(0, 5); | 900 | try list.insert(0, 5); |
| 757 | testing.expect(list.items[0] == 5); | 901 | testing.expect(list.items[0] == 5); |
| 758 | testing.expect(list.items[1] == 1); | 902 | testing.expect(list.items[1] == 1); |
| 759 | testing.expect(list.items[2] == 2); | 903 | testing.expect(list.items[2] == 2); |
| 760 | testing.expect(list.items[3] == 3); | 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" { | 921 | test "std.ArrayList/ArrayListUnmanaged.insertSlice" { |
| 764 | var list = ArrayList(i32).init(testing.allocator); | 922 | const a = testing.allocator; |
| 765 | defer list.deinit(); | 923 | { |
| | 924 | var list = ArrayList(i32).init(a); |
| | 925 | defer list.deinit(); |
| 766 | | 926 | |
| 767 | try list.append(1); | 927 | try list.append(1); |
| 768 | try list.append(2); | 928 | try list.append(2); |
| 769 | try list.append(3); | 929 | try list.append(3); |
| 770 | try list.append(4); | 930 | try list.append(4); |
| 771 | try list.insertSlice(1, &[_]i32{ 9, 8 }); | 931 | try list.insertSlice(1, &[_]i32{ 9, 8 }); |
| 772 | testing.expect(list.items[0] == 1); | 932 | testing.expect(list.items[0] == 1); |
| 773 | testing.expect(list.items[1] == 9); | 933 | testing.expect(list.items[1] == 9); |
| 774 | testing.expect(list.items[2] == 8); | 934 | testing.expect(list.items[2] == 8); |
| 775 | testing.expect(list.items[3] == 2); | 935 | testing.expect(list.items[3] == 2); |
| 776 | testing.expect(list.items[4] == 3); | 936 | testing.expect(list.items[4] == 3); |
| 777 | testing.expect(list.items[5] == 4); | 937 | testing.expect(list.items[5] == 4); |
| 778 | | 938 | |
| 779 | const items = [_]i32{1}; | 939 | const items = [_]i32{1}; |
| 780 | try list.insertSlice(0, items[0..0]); | 940 | try list.insertSlice(0, items[0..0]); |
| 781 | testing.expect(list.items.len == 6); | 941 | testing.expect(list.items.len == 6); |
| 782 | testing.expect(list.items[0] == 1); | 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 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 968 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 787 | defer arena.deinit(); | 969 | defer arena.deinit(); |
| | 970 | const a = &arena.allocator; |
| 788 | | 971 | |
| 789 | const alloc = &arena.allocator; | | |
| 790 | const init = [_]i32{ 1, 2, 3, 4, 5 }; | 972 | const init = [_]i32{ 1, 2, 3, 4, 5 }; |
| 791 | const new = [_]i32{ 0, 0, 0 }; | 973 | const new = [_]i32{ 0, 0, 0 }; |
| 792 | | 974 | |
| 793 | var list_zero = ArrayList(i32).init(alloc); | 975 | const result_zero = [_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }; |
| 794 | var list_eq = ArrayList(i32).init(alloc); | 976 | const result_eq = [_]i32{ 1, 0, 0, 0, 5 }; |
| 795 | var list_lt = ArrayList(i32).init(alloc); | 977 | const result_le = [_]i32{ 1, 0, 0, 0, 4, 5 }; |
| 796 | var list_gt = ArrayList(i32).init(alloc); | 978 | const result_gt = [_]i32{ 1, 0, 0, 0 }; |
| 797 | | 979 | |
| 798 | try list_zero.appendSlice(&init); | 980 | { |
| 799 | try list_eq.appendSlice(&init); | 981 | var list_zero = ArrayList(i32).init(a); |
| 800 | try list_lt.appendSlice(&init); | 982 | var list_eq = ArrayList(i32).init(a); |
| 801 | try list_gt.appendSlice(&init); | 983 | var list_lt = ArrayList(i32).init(a); |
| 802 | | 984 | var list_gt = ArrayList(i32).init(a); |
| 803 | try list_zero.replaceRange(1, 0, &new); | 985 | |
| 804 | try list_eq.replaceRange(1, 3, &new); | 986 | try list_zero.appendSlice(&init); |
| 805 | try list_lt.replaceRange(1, 2, &new); | 987 | try list_eq.appendSlice(&init); |
| 806 | | 988 | try list_lt.appendSlice(&init); |
| 807 | // after_range > new_items.len in function body | 989 | try list_gt.appendSlice(&init); |
| 808 | testing.expect(1 + 4 > new.len); | 990 | |
| 809 | try list_gt.replaceRange(1, 4, &new); | 991 | try list_zero.replaceRange(1, 0, &new); |
| 810 | | 992 | try list_eq.replaceRange(1, 3, &new); |
| 811 | testing.expectEqualSlices(i32, list_zero.items, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }); | 993 | try list_lt.replaceRange(1, 2, &new); |
| 812 | testing.expectEqualSlices(i32, list_eq.items, &[_]i32{ 1, 0, 0, 0, 5 }); | 994 | |
| 813 | testing.expectEqualSlices(i32, list_lt.items, &[_]i32{ 1, 0, 0, 0, 4, 5 }); | 995 | // after_range > new_items.len in function body |
| 814 | testing.expectEqualSlices(i32, list_gt.items, &[_]i32{ 1, 0, 0, 0 }); | 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 | const Item = struct { | 1030 | const Item = struct { |
| ... | @@ -819,11 +1032,25 @@ const Item = struct { | ... | @@ -819,11 +1032,25 @@ const Item = struct { |
| 819 | sub_items: ArrayList(Item), | 1032 | sub_items: ArrayList(Item), |
| 820 | }; | 1033 | }; |
| 821 | | 1034 | |
| 822 | test "std.ArrayList: ArrayList(T) of struct T" { | 1035 | const ItemUnmanaged = struct { |
| 823 | var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(testing.allocator) }; | 1036 | integer: i32, |
| 824 | defer root.sub_items.deinit(); | 1037 | sub_items: ArrayListUnmanaged(ItemUnmanaged), |
| 825 | try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) }); | 1038 | }; |
| 826 | testing.expect(root.sub_items.items[0].integer == 42); | 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 | test "std.ArrayList(u8) implements outStream" { | 1056 | test "std.ArrayList(u8) implements outStream" { |
| ... | @@ -837,19 +1064,32 @@ test "std.ArrayList(u8) implements outStream" { | ... | @@ -837,19 +1064,32 @@ test "std.ArrayList(u8) implements outStream" { |
| 837 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span()); | 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 | // use an arena allocator to make sure realloc returns error.OutOfMemory | 1068 | // use an arena allocator to make sure realloc returns error.OutOfMemory |
| 842 | var arena = std.heap.ArenaAllocator.init(testing.allocator); | 1069 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| 843 | defer arena.deinit(); | 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); | 1076 | try list.append(1); |
| 848 | try list.append(2); | 1077 | try list.append(2); |
| 849 | try list.append(3); | 1078 | try list.append(3); |
| 850 | | 1079 | |
| 851 | list.shrink(1); | 1080 | list.shrink(1); |
| 852 | testing.expect(list.items.len == 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 | test "std.ArrayList.writer" { | 1095 | test "std.ArrayList.writer" { |
| ... | @@ -864,7 +1104,7 @@ test "std.ArrayList.writer" { | ... | @@ -864,7 +1104,7 @@ test "std.ArrayList.writer" { |
| 864 | testing.expectEqualSlices(u8, list.items, "abcdefg"); | 1104 | testing.expectEqualSlices(u8, list.items, "abcdefg"); |
| 865 | } | 1105 | } |
| 866 | | 1106 | |
| 867 | test "addManyAsArray" { | 1107 | test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" { |
| 868 | const a = std.testing.allocator; | 1108 | const a = std.testing.allocator; |
| 869 | { | 1109 | { |
| 870 | var list = ArrayList(u8).init(a); | 1110 | var list = ArrayList(u8).init(a); |