Giter Club home page Giter Club logo

Comments (7)

untoldwind avatar untoldwind commented on August 24, 2024 1

Yes, this could need some optimization. Nevertheless, interesting idea. I already wanted a more restricted form of AnyString.

Maybe one should just pick a table via genParams.Rng.NextIntn(len(table)) and then pick a position via genParams.Rng.NextIntn((table.Hi - table.Lo)/table.Stride)*table.Stride

... I'll play a bit with that

from gopter.

untoldwind avatar untoldwind commented on August 24, 2024 1

Ok, I just merged it.

Concerning the uniform distribution: I'm no so sure if this is really desired. Don't get me wrong here, I don't want to defend my implementation at all cost ;)

The point is that for a test designed to discover edge cases one probably want to have a pick from each table, with a uniform distribution chances are that that the small tables might never get a hit at all.

Maybe there should be an option for both ...

from gopter.

jchildren avatar jchildren commented on August 24, 2024

Looks like one way might be to do something like this:

// UnicodeRangeChar generates arbitrary character runes from a table
func UnicodeRangeChar(table *unicode.RangeTable) gopter.Gen {
	var runes []rune
	for _, runeRange := range table.R16 {
		for i := runeRange.Lo; i < runeRange.Hi; i += runeRange.Stride {
			runes = append(runes, rune(i))
		}
	}
	for _, runeRange := range table.R32 {
		for i := runeRange.Lo; i < runeRange.Hi; i += runeRange.Stride {
			runes = append(runes, rune(i))
		}
	}

	return func(genParams *gopter.GenParameters) *gopter.GenResult {
		var nextIndex int = int(genParams.NextUint64()) % len(runes)
		genResult := gopter.NewGenResult(runes[nextIndex], Int64Shrinker)
		return genResult
	}
}

with

// UnicodeRangeString generates arbitrary string from a table
func UnicodeRangeString(table *unicode.RangeTable) gopter.Gen {
	return genString(UnicodeRangeChar(table), func(r rune) bool { return unicode.Is(table, r) })
}

I'm not sure if this is the best way to do the shrinking, or about effectiveness of the number of append calls, but this looks reasonably promising.

edit: my math may be wrong as well for the indexing of the slice

from gopter.

jchildren avatar jchildren commented on August 24, 2024

After thinking about this further, my implementation is pretty terrible memory wise due to the slice appends.

from gopter.

untoldwind avatar untoldwind commented on August 24, 2024

Maybe you'd like to take a look at #17

IMHO that should pretty much offer the desired functionality

from gopter.

jchildren avatar jchildren commented on August 24, 2024

Looks good to me.

It would still be nice to be able to guarantee a uniform distribution of runes from the table though. Another possibility would be to compute the length of the table, generate a random index, then iterate across the table until we find the rune corresponding to the index.

This could be improved further by creating a slice of the length of each table entry and then performing some kind of search. But that is just premature optimization at this point.

The performance would be worse than your implementation though so I am very happy with #17 in any case.

from gopter.

jchildren avatar jchildren commented on August 24, 2024

Sure, it depends how much we expect the smaller tables to provide edge cases. Probably in most cases the smaller entries will be significant or control characters rather than alphanumeric so your justification is valid. I don't see a need to have two implementations.

from gopter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.