github.com/bytedance/mockey@v1.2.10/internal/monkey/mem/write_test.go (about)

     1  package mem
     2  
     3  // Copyright 2023 2022 ByteDance Inc.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/bytedance/mockey/internal/monkey/common"
    21  	"github.com/bytedance/mockey/internal/tool"
    22  )
    23  
    24  func TestWrite(t *testing.T) {
    25  	data := make([]byte, common.PageSize()*3)
    26  	target := uintptr(common.PageSize() / 2)
    27  
    28  	expected := [][2]uintptr{
    29  		{target, uintptr(common.PageSize())},
    30  		{uintptr(common.PageSize()), uintptr(common.PageSize()) * 2},
    31  		{uintptr(common.PageSize() * 2), uintptr(common.PageSize()) * 3},
    32  		{uintptr(common.PageSize() * 3), target + uintptr(len(data))},
    33  	}
    34  
    35  	begin := target
    36  	end := target + uintptr(len(data))
    37  	index := 0
    38  	for begin < end {
    39  		if common.PageOf(begin) < common.PageOf(end) {
    40  			nextPage := common.PageOf(begin) + uintptr(common.PageSize())
    41  			buf := data[:nextPage-begin]
    42  			data = data[nextPage-begin:]
    43  
    44  			// err := Write(begin, buf)
    45  			// tool.Assert(err == nil, err)
    46  			tool.Assert(begin == expected[index][0], index, begin, expected[index][0])
    47  			tool.Assert(begin+uintptr(len(buf)) == expected[index][1], index, begin+uintptr(len(buf)), expected[index][1])
    48  
    49  			begin += uintptr(len(buf))
    50  			index += 1
    51  			continue
    52  		}
    53  
    54  		// err := Write(begin, data)
    55  		// tool.Assert(err == nil, err)
    56  		tool.Assert(begin == expected[index][0], index, begin, expected[index][0])
    57  		tool.Assert(begin+uintptr(len(data)) == expected[index][1], index, begin+uintptr(len(data)), expected[index][1])
    58  
    59  		break
    60  	}
    61  }